selenium findelement vba blocks the routine

2019-08-25 09:06发布

问题:

this is my code to open a site and search for objects. It may happen that an object is not found and therefore I have to set up a search on its existence or not, so as to perform or not operations. I'm trying this way but if the object I'm looking for, the routine crashes ....

  Dim bot As New SeleniumWrapper.WebDriver
  bot.SetProfile "C:\Users\USER\AppData\Local\Google\Chrome\User Data\Default"
  bot.start "chrome", "https://website.com/"
  bot.Open "/"
  bot.wait 10000
  bot.findElementByClassName("_20NlL").Click
  bot.Wait 1000
  bot.findElementByClassName("C28xL").Click
  bot.Wait 1200
  bot.SendKeys text
  bot.Wait 2000
  Dim notext As WebElement
  Set notext = bot.findElementByClassName("_3WZoe", timeoutms:=0)
  If notext Is Nothing Then
  ......[this is the error that is shown to me][1]

![1]: https://i.stack.imgur.com/wWKzW.jpg

what am I wrong?

if I declare bot as webdriver or chromedriver as indicated by QHarr I get this error unfortunately ....

error run-time 33: session not created exception ecc. i'm using chrome 69.0

回答1:

A number of the errors I can see make me think you have translated this across from another language, or are working with syntax not designed for current selenium basic wrapper.

1) For selenium basic the following timeoutms is not a named argument. The correct named argument is timeout so something is odd with your reported error as you should have received a named argument error message.

2) Simply declare as Dim bot As New WebDriver or New ChromeDriver if using Chrome.

3) The correct method with selenium basic is FindElementByClass


Your sendKeys likely should be focused on a specific element e.g.

bot.findElementByClass("C28xL").SendKeys Text

You can check whether an element is currently present with

bot.findElementByClass("_3WZoe", timeout:=0, Raise:=False)

Again, bearing in mind the point around navigating parent form/frame/iframe tags and wait times.


Ensure you are using the latest Chrome browser and ChromeDriver. The ChromeDriver should be on the environmental path i.e. in a folder which is on the enviromental path and that the path section (for the driver) should be up to, but not including, the .exe (i.e. to the containing folder level). You can alternatively specify the location to look in for the driver as a statement within the code.



回答2:

Set notext = bot.findElementByClassName("_3WZoe", timeoutms:=0)

Your are explicitly setting timeoutms:=0. You're giving it no time to try to find the element on the page. Take that parameter out of your call, give it time to find the element on the page. It's going to run on the default timeout value (don't recall what it is off the top of my head), but should return quickly in most cases. You've already waited 2 seconds from your previous sendKeys command, so it's likely (though not guaranteed) that the element will have loaded by then.


Additionally, it's entirely possible that the element you're looking for resides in a frame somewhere on the page and that you'll have to switch to the proper frame before you ever have a chance of findElementBy<anyoftheoptions> ever working.

That was one of the biggest issues I had was just the lack of understanding enough HTML to get through the web page source quickly and easily enough to make "light work" of the project. I did, with enough trial and error, eventually get there though.


Also, I'd strongly recommend that you wrap the whole thing in some error handling to be able to catch and gracefully recover from errors. You're dealing with web servers and the internet and general traffic outside the realm of your LAN - there will be timeouts. It would be good for your code to not blow up for things that are well beyond your control.