所以,我有一个隐藏在警报下的元素。警报停留 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/