VBA + Selenium不会返回objeck / element的大小

我有一个非常令人沮丧的selenium问题。

我正在使用VBA + Selenium,我试图检查一个元素是否存在。

我正在通过查找元素并检查它的大小来做到这一点。 如果它是0,则意味着它不存在。

我使用下面的代码:

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

我可能使用它是错误的,因为我得到一个“对象不支持这个属性”的错误。 但是,如果我使用:

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

然后我得到所需的文字。

那么我的尺寸代码有什么问题? (我也试过.size(),但我得到相同的错误)

感谢您的帮助提前,周末愉快!

您需要使用FindElement的复数版本,如下所示:

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

我正在使用VBA + Selenium,我试图检查一个元素是否存在。

尝试以下方法

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

答案基于: https : //stackoverflow.com/a/22949100/2517622

你应该使用

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

大小不是一个数字,而是一个具有两个属性的对象。

有时候,如果我和常驻函数没有任何关系来testing元素是否存在,我会这样做。

 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 

这对我有用,帮了我很多。

示例1与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 

示例2与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 

示例3与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 

使用上述示例获取最新版本的date: https : //github.com/florentbr/SeleniumBasic/releases/latest