hunsnowboarder
hunsnowboarder

Reputation: 180

VBA + Selenium won't return size of objeck/element

I am having a very frustrating problem with selenium.

I am using VBA + Selenium and I am trying to check whether an element exists or not.

I am doing it by finding the element and checking it's size. If it is 0 it means that it is not present.

I use the following code:

element_size = driver.FindElementByCss("div.toto > p:nth-child(6)").Size

I am probably using it wrong because I get an "object doesn't support this property" error. However if I use:

element_size = driver.FindElementByCss("div.toto > p:nth-child(6)").Text

Then I get the desired text.

So what is wrong with my size code? ( I also tried .size() but I get the same error)

Thanks for your help in advance and have a nice weekend!

Upvotes: 0

Views: 1977

Answers (5)

You should use

element_height = driver.FindElementByCss("div.toto > p:nth-child(6)").Size.Height
element_width = driver.FindElementByCss("div.toto > p:nth-child(6)").Size.Width

Size is not a numeral, but an object with two properties.

Upvotes: 1

Florent B.
Florent B.

Reputation: 42538

Example 1 with FindElement:

Sub Script1()
    Dim drv As New Selenium.FirefoxDriver
    drv.Get "http://stackoverflow.com"

    Set ele = drv.FindElementByCss("#hlogo", raise:=False, timeout:=0)
    If Not ele Is Nothing Then
      Debug.Print "Element is present"
    End If

    drv.Quit
End Sub

Example 2 with IsElementPresent:

Private By As New Selenium.By

Sub Script2()
    Dim drv As New Selenium.FirefoxDriver
    drv.Get "http://stackoverflow.com"

    If drv.IsElementPresent(By.Css("#hlogo")) Then
      Debug.Print "Element is present"
    End If

    drv.Quit
End Sub

Example 3 with FindElementsByCss:

Sub Script3()
    Dim drv As New Selenium.FirefoxDriver
    drv.Get "http://stackoverflow.com"


    Set elts = drv.FindElementsByCss("#hlogo")
    If elts.Count > 0 Then
      Debug.Print "Element is present"
    End If

    drv.Quit
End Sub

To get the latest version in date working with the above examples: https://github.com/florentbr/SeleniumBasic/releases/latest

Upvotes: 1

jemuzu
jemuzu

Reputation: 1

Sometimes, I do this if I have nothing to do with the resident functions for testing whether an element is present or not.

src = driver.getHtmlSource ' I get all the source string

If Instr(src, "desired string to find") <> 0 Then 'in this case, if your desired string is caught inside the source, this is true. then do stuff below
  'do stuff 
End if 

This works for me and helped me a lot.

Upvotes: 0

drets
drets

Reputation: 2805

I am using VBA + Selenium and I am trying to check whether an element exists or not.

Try following:

number_of_elements = driver.FindElementsByCss("div.toto > p:nth-child(6)").Length

Answer is based on: https://stackoverflow.com/a/22949100/2517622

Upvotes: 1

Mesut GUNES
Mesut GUNES

Reputation: 7421

You need to use the plural version of FindElement as below:

element_size = driver.FindElementsByCss("div.toto > p:nth-child(6)").Size

Upvotes: 1

Related Questions