Excel VBA和x2B;硒元';t返回对象/元素的大小
我对硒有一个非常令人沮丧的问题 我正在使用VBA+Selenium,并尝试检查元素是否存在 我是通过找到元素并检查它的大小来实现的。如果为0,则表示它不存在 我使用以下代码:Excel VBA和x2B;硒元';t返回对象/元素的大小,excel,vba,selenium,Excel,Vba,Selenium,我对硒有一个非常令人沮丧的问题 我正在使用VBA+Selenium,并尝试检查元素是否存在 我是通过找到元素并检查它的大小来实现的。如果为0,则表示它不存在 我使用以下代码: element_size = driver.FindElementByCss("div.toto > p:nth-child(6)").Size 我可能用错了,因为我得到了一个“对象不支持此属性”错误。但是,如果我使用: element_size = driver.FindElementByCss("div.tot
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\u size=driver.findelementsbycs(“div.toto>p:nth child(6)”).size
您需要使用FindElement
的复数版本,如下所示:
element\u size=driver.findelementsbycs(“div.toto>p:nth child(6)”).size
我正在使用VBA+Selenium,我正在尝试检查元素
存在与否
请尝试以下操作:
number_of_elements = driver.FindElementsByCss("div.toto > p:nth-child(6)").Length
答案基于:
我正在使用VBA+Selenium,我正在尝试检查元素
存在与否
请尝试以下操作:
number_of_elements = driver.FindElementsByCss("div.toto > p:nth-child(6)").Length
答案基于:有时,如果我与测试元素是否存在的驻留函数无关,我会这样做
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
src=driver.getHtmlSource'我获取所有源字符串
如果Instr(src,“要查找的所需字符串”)为0,那么在本例中,如果所需字符串在源代码中被捕获,则为true。然后做下面的事情
“做事
如果结束
这对我很有用,也帮了我很大的忙。有时候,如果我与测试元素是否存在的驻留函数无关,我会这样做
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
src=driver.getHtmlSource'我获取所有源字符串
如果Instr(src,“要查找的所需字符串”)为0,那么在本例中,如果所需字符串在源代码中被捕获,则为true。然后做下面的事情
“做事
如果结束
这对我很有用,也帮了我很大的忙。FindElement示例1:
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
IsElementPresent的示例2:
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
使用FindElementsByCss的示例3:
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
要使用上述示例获得最新版本,请执行以下操作:
带有FindElement的示例1:
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
IsElementPresent的示例2:
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
使用FindElementsByCss的示例3:
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
要使用上述示例获得最新版本,请执行以下操作:
您应该使用
element_height = driver.FindElementByCss("div.toto > p:nth-child(6)").Size.Height
element_width = driver.FindElementByCss("div.toto > p:nth-child(6)").Size.Width
大小不是数字,而是具有两个属性的对象。您应该使用
element_height = driver.FindElementByCss("div.toto > p:nth-child(6)").Size.Height
element_width = driver.FindElementByCss("div.toto > p:nth-child(6)").Size.Width
大小不是数字,而是具有两个属性的对象。谢谢您的建议!谢谢你的建议!是的,这就是问题所在!非常感谢!:)是的,这就是问题所在!非常感谢!:)我明白了,你可能使用了不同的包装器,如果你使用的是GitHub的版本,我的解决方案有效,而不是Google代码我明白了,你可能使用了不同的包装器,如果你使用的是GitHub的版本,而不是Google代码,我的解决方案有效