【问题标题】:Clicking an element for Chrome browser an issue when using selenium automation?使用 selenium 自动化时,单击 Chrome 浏览器的元素会出现问题吗?
【发布时间】:2016-11-19 12:16:24
【问题描述】:

driver.findElement(By.xpath("//button")).click();

预期结果:成功点击按钮。

实际结果:原因:org.openqa.selenium.ElementNotVisibleException:元素必须显示才能点击(警告:服务器未提供任何堆栈跟踪信息);”。

【问题讨论】:

  • 你是什么意思这是 Chrome 浏览器的问题,而异常清楚地表明元素必须显示才能点击..所以在点击之前你需要检查 isDisplayed() 返回真或假...
  • 像已经提到的其他人一样,请手动检查按钮是否可见(通过您的眼睛)。如果它不可见,请在单击之前执行一些操作使其可见。

标签: java google-chrome selenium testing automation


【解决方案1】:

点击前需要检查isDisplayed()isEnabled()如下:-

WebElement el = driver.findElement(By.xpath("//button"));
if(el.isDisplayed() && el.isEnabled())
{
   el.click();
}

或者您应该尝试使用WebDriverWaitExpectedConditions.elementToBeClickable,如下所示:-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button")));
el.click();

注意 :- elementToBeClickable 用于检查元素是否可见并已启用,以便您可以单击它。

WebDriverWaitExpectedConditions.elementToBeClickable 如果预期条件为真,则返回WebElement,否则将抛出TimeoutException

希望对您有所帮助...:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多