【问题标题】:Another button that is not clicking另一个没有点击的按钮
【发布时间】:2020-01-26 23:30:32
【问题描述】:

以下是我尝试使用各种选项单击但不起作用的按钮的 HTML:

<button data-ng-click="Question.setAnswer(button.value,button.attemptNext)" class="btn btn-sm btn-primary " type="button">No</button>

我尝试了以下

new WebDriverWait(driver, 0).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='btn btn-sm btn-primary' and @value='No']"))).click();

但它不起作用 TIA

这是控制台信息。 在添加该行之前,我一直在看到 webdriver 错误 只允许本地连接。 请保护 ChromeDriver 和相关测试框架使用的端口,防止恶意代码访问。 2020 年 1 月 27 日下午 1:03:02 org.openqa.selenium.remote.ProtocolHandshake createSession 信息:检测到的方言:W3C 发生 WebDriverException

【问题讨论】:

  • 这里是 HTML

标签: selenium selenium-webdriver xpath


【解决方案1】:

No 是文本,而不是值。使用

new WebDriverWait(driver, 0).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='btn btn-sm btn-primary' and .='No']"))).click();

【讨论】:

    【解决方案2】:

    你似乎很接近。 &lt;button&gt; 元素没有 value 属性,但将 innerText 设置为 No

    此外,0 期间的 WebDriverWait 不是理想配置,必须设置为正值。

    但是,所需的元素是 Angular 元素,因此要定位/与您必须为 element_to_be_clickable() 诱导 WebDriverWait 的元素交互,您可以使用以下任一解决方案:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-sm.btn-primary[data-ng-click*='attemptNext']"))).click()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-sm btn-primary ' and text()='No']"))).click()
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    更新

    我没有从您提供的堆栈跟踪中看到任何此类错误。由于您仍然无法单击诱导WebDriverWait 的元素作为替代,您可以使用execute_script(),如下所示:

    • cssSelector:

      element_css = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-sm.btn-primary[data-ng-click*='attemptNext']")))
      driver.execute_script("arguments[0].click();", element_css)
      
    • xpath:

      element_xpath = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-sm btn-primary ' and text()='No']")))
      driver.execute_script("arguments[0].click();", element_xpath)
      

    【讨论】:

    • 感谢您的回复,我尝试了解决方案,但它没有点击按钮。 new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-sm btn-primary ' and text()='No'']")))。点击();
    • @JOE_D 您看到任何错误吗?请问错误堆栈跟踪?
    • 我对 selenium eclipse 很陌生,你能告诉我如何做错误堆栈跟踪吗?在控制台上它没有显示太多信息
    • @JOE_D 控制台中出现的任何信息都将它们复制并添加到您的问题中。我们可以拦截相关的错误。
    • 我刚刚在问题中添加了控制台消息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2016-09-26
    相关资源
    最近更新 更多