SeleniumBasic
SeleniumBasic copied to clipboard
.IsElementPresent is not wokring in VBA
Hello Pals
I'm finding the trouble while using the ".IsElementPresent" feature in VBA Excel.
Here are my System Details
Chrome Browser Version 65.0.3325.181 (Official Build) (64-bit) Latest and Updated Selenium References and Chrome Driver MS Office 2016 32bit
Well Actually I wanna to run a query that, In a loading page on Chrome Browser, if XYZ element will not Found or Present then Do Process 1 else Do Process 2
But I'm not able to set the Proper Syntax.
Every time I'm getting an error
Object Not Found
Here is the piece of my code:
If bot.IsElementPresent(By.XPath("//*[@id='main']/footer/div[1]/div[2]/div/div[2]"), 1000) Then
Application.SendKeys ("~")
bot.wait 2000
ThisWorkbook.Sheets("Sheet1").Cells(rng, 3).Value = "YES"
rng = rng + 1
wait = 2
Else
GoTo bypass
End If
Thank you in advance pals.
That problems ocurred with me too , so i come with 2 solutions.
Solution number 1 -
bot.wait(2000) if bot.findelements(By.XPath("//*[@id='main']/footer/div[1]/div[2]/div/div[2]")).count > 0 then 'THE ELEMENT EXIST else ' THE ELEMENT NOT EXIST end if
Solution number 2 -
public function waitelempresent(byref driver as webdriver , byval By as By , byval optional timeout as long = 40000) waitelempresent = false dim i as long for i = 0 to (timeout / 1000) if driver.findelements(by).count > 0 then waitelempresent = true : exit function next i end function
I write that Right now so maybe doesnt work , because i not compiled it , The code is not there with me , is it in my Work Place Sorry.
Thanks, pal, for your comment.
I tried your solution No. 1 and still getting the error.
error information:
I'm trying your second solution now, Please tell me where I'm making a mistake? And Why they are requireing a Object?
Thank you again.
Dear Ronanvico,
I found the solution, by trial and error method. But Your ".count = 0" would be game changer for me in this troubleshooting.
here is the code.
Sub iselementpresenttest()
Dim bot As New WebDriver
bot.Start "chrome", "https://www.google.com/"
bot.Window.Maximize
bot.Get "/"
If bot.FindElementsByXPath("//*[@id='tsf']/div[2]/div[3]/center/input[2]").Count > 0 Then
bot.Quit
MsgBox "Yes"
Else
bot.Quit
MsgBox "NO"
End If
End Sub
What Exactly I'm trying with above code is,
- Open the Browser
- Maximise the Window
- Run the Google.com Address
- And want to find the whether "I'm Feeling Lucky" button is present or not!
It is working for me. Let me try more tasks! Suggest me more possibilities.
I wanna to know, how to find elements and possibilities while executing JavaScript through VBA?
Thank you again!
Hi , that worked ? Thats awesome dude , i use .count a lot in my code ...
I can build more solutions when i get some free time , ok ? I create some nice functiona for selenium who is pretty good and works fine , i am using them on my projects at work , on big projects! And they are doing fine...
If u want i can creatw a module and put on my github , them people can use this codes and contribuit or help me implement more codes! What do you think about it ???
Im writting from my iphone , while im going to my work, so sorry for cant help u right now!
Have a good day !
Yup brother. that's worked. I need to change the syntax minorly. But ".count" helped me a lot.
And Please Go ahead, put your module here So, we can learn more and then we could be able to input more.
anyway, have you got the solution to install selenium libraries without Exe?
thank you again.
Here is the code I use. When you have situations like always use functions and On Error. It is much easier. Also if it succeeds you have element and text value. I've shown a few functions. I use user defined types to hold values. It makes code much cleaner.
Dim drv As New Selenium.ChromeDriver
Type selUT
text As String
xPath As String
ele As WebElement
eleEach As WebElements
End Type
Sub Test()
Dim k As selUT
drv.Get "https://www.google.com/"
k.xPath = "//*[@id='tsf']/div[2]/div[3]/center/input[2]"
If GetElement(k) Then
MsgBox "Got Element"
Else
MsgBox "No Element"
End If
End Sub
Function GetElement(k As selUT) As Boolean
On Error GoTo Handler
k.text = ""
Set k.ele = drv.FindElementByXPath(k.xPath)
k.text = k.ele.text
GetElement = True
Exit Function
Handler:
Err.Clear
GetElement = False
End Function
Function GetElementEach(k As selUT) As Boolean
On Error GoTo Handler
k.text = ""
Set k.ele = k.eleEach.FindElementByXPath(k.xPath)
k.text = k.ele.text
GetElementEach = True
Exit Function
Handler:
Err.Clear
GetElementEach = False
End Function
Function GetElementClick(k As selUT) As Boolean
On Error GoTo Handler
k.text = ""
Set k.ele = drv.FindElementByXPath(k.xPath)
k.ele.Click
drv.Wait (1000)
GetElementClick = True
Exit Function
Handler:
Err.Clear
GetElementClick = False
End Function
I liked your idea , maybe i build something like it ,
How did you used chrome ?, i tried to use it , i downloaded the last version and put on user\seleniumbasic\ , but when i run it just appears an error , and on chrome keep showing a message “chrome is being used by a automation process”
Sorry guys i forgot to build the repository , but i will create thia week and we can keep selenium basic alive
It shows message but still works for me.
Windows 10, v1709 x64
Chrome Version 65.0.3325.181
Latest Driver
If driver.IsElementPresent(By.XPath("/html/body/dat/div/xx-xx")) = True Then myPause (5) Else driver.FindElementByCss("span.mi.mi--insert-file").Click End If
Public Sub myPause(Optional nQtdSeconds As Double = 3)
Dim nStart As Double
nStart = Timer
Do While Timer < nStart + nQtdSeconds
DoEvents
Loop
End Sub
I'm leaving this as a note for myself. There is an object missing if I follow OP syntax. The missing object is By.
The solution is to add the following line before the OP's code snippet:
Dim By As New By