出现异常:元素不可单击,因为另一个元素将其遮盖了

时间:2018-07-11 08:49:19

标签: java selenium webdriver

package Roughpack;

import java.util.List;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;

import org.openqa.selenium.support.ui.WebDriverWait;

public class MyClass {

    public static void main(String[] args) {

        System.setProperty("webdriver.gecko.driver","D:\\Executabel\\geckodriver-v0.21.0-win64\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver();

        WebDriverWait wait = new WebDriverWait(driver, 20);

        driver.get("http://pro.tykitksa.com/");

        driver.manage().window().maximize();

        driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);

        List<WebElement> dropDownList = driver.findElements(By.id("user_event_city"));

        System.out.println(dropDownList.size());

        for (int i = 0; i < dropDownList.size(); i++) {

            System.out.println(dropDownList.get(i).getText());

            WebElement Dropdown = driver.findElement(By.id("user_event_city"));

            Select select = new Select(Dropdown);

            select.selectByIndex(4);

        }

    }
}

1 个答案:

答案 0 :(得分:0)

您需要添加等待cityModal Webelement,因为在页面加载时您的下拉菜单清晰可见:

System.setProperty("webdriver.gecko.driver","D:\\Executabel\\geckodriver-v0.21.0-win64\\geckodriver.exe");

    WebDriver driver = new FirefoxDriver();

    WebDriverWait wait = new WebDriverWait(driver, 20);

    driver.get("http://pro.tykitksa.com/");

    driver.manage().window().maximize();

    driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);

//this is wait for se-pre-con-home element will be invisible        
wait.until(ExpectedConditions.invisibilityOf(driver.findElement(By.xpath("//div[@class=\"se-pre-con-home\"]")))); 

    List<WebElement> dropDownList = driver.findElements(By.id("user_event_city"));

    System.out.println(dropDownList.size());

    for (int i = 0; i < dropDownList.size(); i++) {

        System.out.println(dropDownList.get(i).getText());

        WebElement Dropdown = driver.findElement(By.id("user_event_city"));

        Select select = new Select(Dropdown);

        select.selectByIndex(4);