Ajax Selenium WebDriver-确定元素是否可单击(即不被dojo modal lightbox遮挡)
我编写自动化脚本来测试非常依赖ajax的web应用程序。例如,保存设置时,将显示一个模式对话框,其中包含文本“Ajax Selenium WebDriver-确定元素是否可单击(即不被dojo modal lightbox遮挡),ajax,selenium,dojo,webdriver,Ajax,Selenium,Dojo,Webdriver,我编写自动化脚本来测试非常依赖ajax的web应用程序。例如,保存设置时,将显示一个模式对话框,其中包含文本“保存…””,而灯箱将灰显页面的其余部分 我的测试脚本正在尝试在消息消失之前单击测试中的下一个链接。它在驱动Firefox时几乎总是起作用,但在驱动Chrome时会显示以下错误: Exception in thread "main" org.openqa.selenium.WebDriverException: Element is not clickable at point (99.5
保存…”
”,而灯箱将灰显页面的其余部分
我的测试脚本正在尝试在消息消失之前单击测试中的下一个链接。它在驱动Firefox时几乎总是起作用,但在驱动Chrome时会显示以下错误:
Exception in thread "main" org.openqa.selenium.WebDriverException: Element is not clickable at point (99.5, 118.5). Other element would receive the click: <div class="dijitDialogUnderlay _underlay" dojoattachpoint="node" id="lfn10Dijit_freedom_widget_common_environment_Dialog_8_underlay" style="width: 1034px; height: 1025px; "></div> (WARNING: The server did not provide any stacktrace information)
线程“main”org.openqa.selenium.WebDriverException中的异常:元素在点(99.5118.5)处不可单击。其他元素将收到单击:(警告:服务器未提供任何stacktrace信息)
发生这种情况是因为灯箱遮挡了我要单击的元素。我想等待lightbox消失,然后再尝试单击链接
检查灯箱是否不再存在不是一个有效的解决方法,因为有时存在多个级别的模式对话框和灯箱,并且没有简单的方法来区分它们
在Selenium中是否有方法检测元素是否可点击(没有其他元素遮挡它)?try/catch可能是一个解决方法,但如果可能的话,我更愿意做一个适当的检查。您可以创建一个
clickUntil
函数/方法,让WebDriver等待元素被单击并超时。它将尝试单击该元素,并在每次单击或超时之前丢弃“元素不可单击”错误消息
不知道如何在dojo中编写,但这是一个想法。使用WebDriverWait条件
WebDriverWait wait = new WebDriverWait(yourWebDriver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//xpath_to_element")));
Webdriver将等待5秒钟,以便您的元素能够被单击。您可以使用
ExpectedConditions.invisibilityOfElementLocated(By)
方法,该方法将等待元素在DOM上不可见或不存在
WebDriverWait wait = new WebDriverWait(yourWebDriver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("yourSavingModalDialogDiv")));
因此,根据模态对话框不可见或脱离DOM所需的时间,webdriver将等待。等待时间最长为10秒。在Scala中:
while (remote.findElement(locator).isDisplayed) {
println(s"isDisplayed: $ii $a : " + remote.findElement(locator).isDisplayed)
Thread.sleep(100)
}
val oldtimestamp = remote.findElements(locator).get(0).getAttribute("data-time-stamp")
while (oldtimestamp == remote.findElements(locator).get(0).getAttribute("data-time-stamp")) {
println("Tstamp2:" + remote.findElements(locator).get(0).getAttribute("data-time-stamp"))
Thread.sleep(200)
}
我也有同样的问题,但我在网站上测试了很多输入。一个是我测试过的可点击的,另一个是不可点击的。我是用try()catch()做的 简单代码:
for(){ // for all my input
try {
driver.findElement(By.xpath("...."
+ "//input)["+(i+1)+"]")).click();
... tests...
} catch(Exception e) {
if(e.getMessage().contains("is not clickable at point")) {
System.out.println(driver.findElement(By.xpath(locator)).
getAttribute("name")+" are not clicable");
} else {
System.err.println(e.getMessage());
}
}
更加优雅:
@SuppressWarnings("finally")
public boolean tryClick(WebDriver driver,String locator, locatorMethods m) {
boolean result = false;
switch (m) {
case xpath:
try {
driver.findElement(By.xpath(locator)).click();
result= true;
} catch (Exception e) {
if(e.getMessage().contains("is not clickable at point")) {
System.out.println(driver.findElement(By.xpath(locator)).getAttribute("name")+" are not clicable");
} else {
System.err.println(e.getMessage());
}
} finally {
break;
}
case id:
try {
driver.findElement(By.id(locator)).click();
return true;
} catch (Exception e) {
if(e.getMessage().contains("is not clickable at point")) {
System.out.println(driver.findElement(By.id(locator)).getAttribute("name")+" are not clicable");
} else {
System.err.println(e.getMessage());
}
} finally {
break;
}
default:
System.err.println("Unknown locator!");
}
return result;
}
上一次我用Chrome试用时,它不起作用:“可点击”条件得到满足,尽管有其他层阻止了点击。不使用Firefox 20,selenium Firefox驱动程序2.31.0。当元素被模式对话框阻止时,elementToBeClickable返回true。2.31的发行说明中没有关于Firefox 20的任何内容。它在Firefox 19中有效吗?@PavelZorin 2.32甚至无法启动Firefox 20,这就是我被迫使用2.31.0的原因。你试过你的解决办法吗?用什么驱动程序?什么版本?我不知道为什么OP会接受此选项。
ExpectedConditions。ElementToBeClickable
检查元素是否为空、是否显示和是否已启用-没有一个句柄被另一个元素覆盖。
@SuppressWarnings("finally")
public boolean tryClick(WebDriver driver,String locator, locatorMethods m) {
boolean result = false;
switch (m) {
case xpath:
try {
driver.findElement(By.xpath(locator)).click();
result= true;
} catch (Exception e) {
if(e.getMessage().contains("is not clickable at point")) {
System.out.println(driver.findElement(By.xpath(locator)).getAttribute("name")+" are not clicable");
} else {
System.err.println(e.getMessage());
}
} finally {
break;
}
case id:
try {
driver.findElement(By.id(locator)).click();
return true;
} catch (Exception e) {
if(e.getMessage().contains("is not clickable at point")) {
System.out.println(driver.findElement(By.id(locator)).getAttribute("name")+" are not clicable");
} else {
System.err.println(e.getMessage());
}
} finally {
break;
}
default:
System.err.println("Unknown locator!");
}
return result;
}