使用selenium自动化时,单击Chrome浏览器的元素会出现问题?

时间:2016-07-16 10:57:34

标签: java google-chrome selenium testing automation

driver.findElement(By.xpath(" //按钮&#34))。单击();

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

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

1 个答案:

答案 0 :(得分:0)

您需要先检查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用于检查元素是否可见并已启用,以便您可以单击它。

{p> WebDriverWait如果预期条件为真,则ExpectedConditions.elementToBeClickable会返回WebElement,否则会抛出TimeoutException

希望它有帮助...:)