我使用了明确的等待,并发出警告:
如果我使用Thread.sleep(2000)
,则不会收到任何警告。
@Test(dataProvider = "menuData")
public void Main(String btnMenu, String TitleResultPage, String Text) throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.findElement(By.id("navigationPageButton")).click();
try {
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(btnMenu)));
} catch (Exception e) {
System.out.println("Oh");
}
driver.findElement(By.cssSelector(btnMenu)).click();
Assert.assertEquals(driver.findElement(By.cssSelector(TitleResultPage)).getText(), Text);
}
最佳答案
WebDriverException:元素在点(x,y)不可单击
这是典型的org.openqa.selenium.WebDriverException,它扩展了java.lang.RuntimeException。
此异常的字段是:
protected static final java.lang.String BASE_SUPPORT_URL
public static final java.lang.String DRIVER_INFO
public static final java.lang.String SESSION_ID
关于您的个人用例,错误说明了一切:
WebDriverException: Element is not clickable at point (x, y). Other element would receive the click
从代码块中很明显,您已经将
wait
定义为WebDriverWait wait = new WebDriverWait(driver, 10);
,但是您需要像click()
一样在ExplicitWait
发挥作用之前在元素上调用until(ExpectedConditions.elementToBeClickable)
方法。解
错误
Element is not clickable at point (x, y)
可能由不同因素引起。您可以通过以下任一过程解决它们:1.由于存在JavaScript或AJAX调用而无法单击元素
尝试使用
Actions
类:WebElement element = driver.findElement(By.id("navigationPageButton"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
2.元素未单击,因为它不在Viewport内
尝试使用
JavascriptExecutor
将元素带入视口(viewport)中:WebElement myelement = driver.findElement(By.id("navigationPageButton"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement);
3.在元素可单击之前,页面正在刷新。
在这种情况下,请诱导ExplicitWait,即 WebDriverWait ,如第4点所述。
4.元素存在于DOM中,但不可单击。
在这种情况下,通过将
ExpectedConditions
设置为elementToBeClickable
来诱导ExplicitWait,以使元素可单击:WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));
5.元素存在但具有临时覆盖。
在这种情况下,通过将
ExplicitWait
设置为 ExpectedConditions
来诱导 invisibilityOfElementLocated
,以使叠加层不可见。WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));
6.存在元素,但具有永久覆盖。
使用
JavascriptExecutor
直接在元素上发送点击。WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);