如果它在调试中起作用,则表示覆盖层自动消失。您可以等待它消失
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("[id^='device-card']")));
在任何情况下,您都可以等待按钮被点击
button = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Open Door')")));
button.click();
,
它看起来像:
driver.executeScript("document.querySelector('button.btn').click()")
只需调整CSS
,
您需要注意以下几点:
您可以在What is the most efficient way to wait for a page element (xpath) to show up in Selenium Webdriver?
中找到详细的讨论
更新
由于您仍然看到错误 ... ElementClickInterceptedException:元素单击被拦截... ,因此,您可以添加其他步骤来为引入 WebDriverWait 不可见性 ,您可以使用以下任一选项:
-
invisibilityOf(WebElement element)
:
new WebDriverWait(driver,20).until(ExpectedConditions.invisibilityOf(overlapping_webelement));
new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_clickable_element"))).click();
-
invisibilityOfElementLocated(By locator)
:
new WebDriverWait(driver,20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("css_overlapping_element")));
new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_clickable_element"))).click();
您可以在Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working
中找到相关的详细讨论
,
-
您可以将鼠标移到该元素上,然后单击它,即使我面临此问题并且此解决方案也可以解决该问题
Actions clickOnBtn = new Actions(driver);
clickOnBtn .moveToElement("//button[contains(text(),'Open Door')").click().build().perform;
-
即使在屏幕上看不到该元素时,也会发生这种情况,在这种情况下,您可以滚动到该元素,然后使用上面的代码,如下所示
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()",ele);
Actions clickOnBtn = new Actions(driver);
clickOnBtn .moveToElement("//button[contains(text(),'Open Door')").click().build().perform;
使用javascriptExecutor -
click元素
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();",ele);
-
有时甚至会以我们选择元素的方式发生这种情况,可以更改xpath,我们可以尝试使用父标签而不是按钮并尝试
,
您可以通过点击操作定义自己的ExpectedCondition:
public class SuccessfulClick implements ExpectedCondition<Boolean> {
private WebElement element;
public SuccessfulClick(WebElement element) { //WebElement element
this.element = element;
}
@Override
public Boolean apply(WebDriver driver) {
try {
element.click();
return true;
} catch (ElementClickInterceptedException | StaleElementReferenceException | NoSuchElementException e) {
return false;
}
}
}
然后使用它:
wait10.until(elementToBeClickable(btn));
wait10.until(new SuccessfulClick(btn));
本文链接:https://www.f2er.com/3126766.html