【发布时间】:2021-04-05 22:42:26
【问题描述】:
我遇到了这样一种情况,如果一个元素存在,我需要做一些事情,如果该元素不存在,我将需要跳到代码的其余部分。我尝试了几种方法,但我不知道为什么它不起作用,代码对我来说看起来很合乎逻辑并且它适用于类似的宏。 代码如下:
Do
DoEvents
Loop Until driver.ExecuteScript("document.readystate") <> "complete"
If driver.FindElementByName("x").IsPresent Then
Item.Offset(0, 2).Value = "Completed already" 'And need to skip to next iteration
ElseIf driver.FindElementByName("x") Is Nothing Then
Set cancel= driver.FindElementByName("cancelbutton")
cancel.Click
Set myVar= Item.Offset(0, 1)
Set radiobtn = driver.FindElementByXPath("//input[@value='" & myVar & "']")
radiobtn.Click
Set cancelSelected = driver.FindElementByName("submitCancel")
cancelSelected.Click
driver.SwitchToAlert.Accept
Item.Offset(0, 2).Value = "Canceled"
driver.Refresh
driver.Wait 1000
End If
我也尝试过如果元素 X 存在,则查找其他元素的情况
If driver.FindElementByName("x").IsPresent Then
Item.Offset(0, 2).Value = "Completed already"
ElseIf driver.FindElementByName("Y").IsPresent Then
Set cancel= driver.FindElementByName("cancelbutton")
cancel.Click
Set myVar= Item.Offset(0, 1)
Set radiobtn = driver.FindElementByXPath("//input[@value='" & myVar & "']")
radiobtn.Click
Set cancelSelected = driver.FindElementByName("submitCancel")
cancelSelected.Click
driver.SwitchToAlert.Accept
Item.Offset(0, 2).Value = "Canceled"
driver.Refresh
driver.Wait 1000
End If
当元素 X 出现在页面上时,元素 Y 不存在,反之亦然。我也试过.count > 0,但还是不行。任何人都可以帮助我解决问题或给我一些提示和技巧吗?
【问题讨论】:
-
请问有我们可以使用的 url 来解决这个问题吗?
-
当然,请为场景提供合适的 x,y 值
-
很遗憾,即使我分享了一个 URL,您也看不到它,因为它与业务相关并由 VPN 和 2factor 身份验证器保护..
-
请提供更多详细信息,然后说明您尝试了什么以及每种情况下的确切结果是什么,包括完整的任何错误消息。如果使用并与错误消息相关,请包括您添加的显式等待时间,
-
readystate 不会等到所有元素都加载完毕。它不如内置的隐式 webdriver 等待可靠。如果抛出错误,为什么不设置一个处理该场景的错误处理部分,然后在旁边有一个简历带您返回?只要您在错误处理程序之前有一个退出子程序,并且在错误处理程序中进行适当的 err.Number 测试就可以了,即使用错误或缺少错误来分支您的代码。记得重置错误。
标签: vba selenium automated-tests web-testing webautomation