我想在Selenium测试期间单击“确定”按钮,但元素不可见。
driver.findElement(By.xpath("//*[@id=\"5f6e7b16-0fa1-4db6-869b-3a6ba6b0fafe\"]")).click();
<div class="bootstrap-dialog-footer-buttons">
<button class="btn btn-default" id="5a4bb849-7a61-4603-9ef2-f9e0ecab4523">
<span class="glyphicon glyphicon-ban-circle"></span> Cancel
</button>
<button class="btn btn-warning" id="f7f4b18b-2ba2-4c1e-b541-a254c080f398">
<span class="glyphicon glyphicon-ok"></span> Ok
</button>
</div>
答案 0 :(得分:0)
我认为在你的DOM中,按钮id正在动态变化。每当页面重新加载时,它将生成新的id。您在Selenium代码和HTML中使用了不同的按钮ID。所以,我建议你选择className
。尝试下面的代码,希望它适合你。
//If the Element is not visible then wait until that element is not visible
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.className("btn btn-warning")));
//If the element is visible but not Clickable then wait until that element get Clickable.
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.className("btn btn-warning")));
//Then simply click the button
driver.findElement(By.className("btn btn-warning")).click();
答案 1 :(得分:0)
使用JavascriptExecutor单击元素
参考代码,
WebElement element = driver.findElement(By.xpath("//*[@id=\"5f6e7b16-0fa1-4db6-869b-3a6ba6b0fafe\"]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
答案 2 :(得分:0)
根据 HTML 您已共享,所需元素似乎位于 Bootstrap模式对话框中,且该元素的nil
属性是动态的。因此,要调用id
,您必须按如下方式诱导 WebDriverWait :
cssSelector :
click()
xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.bootstrap-dialog-footer-buttons button.btn.btn-warning"))).click();