java - Selenium + Java 的 elementToBeClickable 问题

标签 java selenium

所以,我有一个隐藏在警报下的元素。警报停留 10 秒,之后用户可以单击该元素。这是我处理这种情况的代码:

WebElement create = driver.findElement(By.cssSelector("div.action_menu_trigger"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(create));
create.click();

但是一旦 WebDriver 到达这里,我就会收到此异常,似乎 Selenium 并不关心等待方法:
org.openqa.selenium.ElementClickInterceptedException:
Element <div class="action_menu_trigger"> is not clickable at point (1710.224952697754,140) because another element <div class="noty_body"> obscures it
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:24:21.231Z'

我曾尝试使用 Thread.sleep(10000)它工作正常,但我不想使用 sleep 。

最佳答案

这里的问题是,就 Selenium 而言,警报下的元素是可点击的。它是可见的并已启用,因此它应该是可点击的。您的代码等待元素可点击(假设它将等待警报消失)但 Selenium 已经认为该元素是可点击的,因此它立即尝试点击导致错误消息,因为警报仍然存在并阻止点击。

解决这个问题的方法是等待警报出现然后消失,等待元素可点击,然后点击它。我不知道我有所有的定位器,但下面的代码应该让你指向正确的方向。

// define locators for use later
// this also makes maintenance easier because locators are in one place, see Page Object Model
By alertLocator = By.cssSelector("div.noty_body");
By createLocator = By.cssSelector("div.action_menu_trigger");

// do something that triggers the alert

// wait for the alert to appear and then disappear
WebDriverWait shortWait = new WebDriverWait(driver, 3);
WebDriverWait longWait = new WebDriverWait(driver, 30);
shortWait.until(ExpectedConditions.visibilityOfElementLocated(alertLocator));
longWait.until(ExpectedConditions.invisibilityOfElementLocated(alertLocator));

// now we wait for the desired element to be clickable and click it
shortWait.until(ExpectedConditions.elementToBeClickable(createLocator)).click();

关于java - Selenium + Java 的 elementToBeClickable 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51654804/

相关文章:

java - 如何消除 Android 按钮的延迟?

java - 从 Hijri 日期到 Java 中的 Gregorian

java - 循环返回值

eclipse - "testng doesn' t 支持嵌套的 "java"元素。”- 执行 build.xml 时 - JAR 目标

python - 等待页面重定向 Selenium WebDriver (Python)

c# - 当 driver.Navigate().GoToUrl ("http://www.example.com/"时 chromedriver.exe 已停止工作)

java - 为什么我的位置是0?什么时候应该是1

java - 使用 Spring 3 MVC 的问题

java - 无法定位元素

javascript - 如何使用 R 从国家文件馆 (archives.gov) 中抓取目录系列中的所有文件