Selenium + Java的elementToBeClickable问题

时间:2018-08-02 13:23:31

标签: 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),它可以正常工作,但是我不想使用睡眠。

4 个答案:

答案 0 :(得分:4)

这里的问题是,据Selenium所知,警报 IS 下的元素可单击。它是可见的并已启用,因此应可单击。您的代码等待该元素可单击(假设它将等待警报消失),但是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();

答案 1 :(得分:0)

代替在elementToBeClickable中提供web元素,只需为定位器提供css选择器即可。

您正在尝试在等待之前找到元素。

所以代码如下:

create = wait.until(ExpectedConditions.visibilityOfElement(By.cssSelector(“your css selector”)));  
create.click();

答案 2 :(得分:0)

您使用WebDriverWait的方式不正确。这个:

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

应该是这样的:

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

如@cruisepandey所述,

WebElement create = driver.findElement(By.cssSelector("div.action_menu_trigger"));

定位元素,然后WebDriverWait无效。

编辑: ,您可以尝试使用此功能:

int i = 0;
while (true){
    i++;
    WebElement create = driver.findElement(By.cssSelector("div.action_menu_trigger"));
    try {
        create.click();
        break;
    }catch (Exception e){
        if (i > 30){
            create.click(); // throws exception after 15 sec to prevent infinite loop
        }
        Thread.sleep(500); // pause 0.5 sec
        // try one more time
    }
}

答案 3 :(得分:0)

可以使用Actions对象解决“无法点击的问题”:

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