Selenium Webdriver - 等到用户单击没有 ID 的按钮

问题描述

我在 Chrome 中使用 Selenium Webdriver 和 Excel VBA 自动化,但在等待用户点击按钮时遇到了一些问题。

网页代码如下:

web code

我尝试过 FindElementByCss、FindElementByID...,还有 IsPresent、IsEnabled...但没有任何效果。现在小米代码如下:

t = Timer
    do while bot.FindElementById("ui-button-text").IsPresent = True
        bot.Wait 500
        If Timer - t = 10 Then Exit Do 'Para evitar bucle infinito
    Loop

提前致谢!

解决方法

根据 HTML:

66044675

ui-button-textclass 属性的值,但不是 id 属性的值,并且有多个 {{ 1}} 元素的 <span> 属性值为 class,具有不同的 textContent


要验证元素的存在,您可以使用以下 Locator Strategies

  • 文本为Cancelar的元素:

    • 使用 xpath

      ui-button-text
  • 文本为Repetir的元素:

    • 使用 xpath

      Do While bot.FindElementByXPath("//span[@class='ui-button-text' and text()='Cancelar']").IsPresent = True
      
  • 文本为 Aceptar 的元素:

    • 使用 xpath

      Do While bot.FindElementByXPath("//span[@class='ui-button-text' and text()='Repetir']").IsPresent = True
      

要验证元素是否启用,您可以使用以下Locator Strategies

  • 文本为Cancelar的元素:

    • 使用 xpath

      Do While bot.FindElementByXPath("//span[@class='ui-button-text' and text()='Aceptar']").IsPresent = True
      
  • 文本为Repetir的元素:

    • 使用 xpath

      Do While bot.FindElementByXPath("//span[@class='ui-button-text' and text()='Cancelar']").IsEnabled = True
      
  • 文本为 Aceptar 的元素:

    • 使用 xpath

      Do While bot.FindElementByXPath("//span[@class='ui-button-text' and text()='Repetir']").IsEnabled = True