Selenium + Java 的 elementToBeClickable 问题

the*_*Guy 3 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();
Run Code Online (Sandbox Code Playgroud)

但是一旦 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'
Run Code Online (Sandbox Code Playgroud)

我已经尝试过使用Thread.sleep(10000)并且效果很好,但我不想使用睡眠。

Jef*_*ffC 7

这里的问题是,就 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();
Run Code Online (Sandbox Code Playgroud)