在Java中使用Selenium单击动态下拉列表div

在Java中使用Selenium单击动态下拉列表div,java,selenium,web-crawler,selenium-chromedriver,Java,Selenium,Web Crawler,Selenium Chromedriver,我在从动态生成的下拉列表中指定第一个元素的xpath时遇到问题。在输入一些文本后,我想单击下拉列表中的第一条建议。但是,我想要定位它的方式会导致NoTouchElementException。我的代码: public static void printTickets() throws IOException { System.setProperty("webdriver.chrome.driver", CHROMEDRIVER_PATH); WebDriver driver =

我在从动态生成的下拉列表中指定第一个元素的xpath时遇到问题。在输入一些文本后,我想单击下拉列表中的第一条建议。但是,我想要定位它的方式会导致
NoTouchElementException
。我的代码:

public static void printTickets() throws IOException {
    System.setProperty("webdriver.chrome.driver", CHROMEDRIVER_PATH);
    WebDriver driver = new ChromeDriver();
    driver.get("https://bilkom.pl/");

    // hide iframe
    WebElement closeFrameButton = driver.findElement(By.xpath("//div[@class='modal-body']//button[@class='close']"));
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(closeFrameButton));
    closeFrameButton.click();

    // fill first field
    WebElement textInput = driver.findElement(By.xpath("//input[@id='fromStation']"));
    textInput.sendKeys("Warszawa");
    String firstElementXPath = "//div[@id='fromStation-cg']//div[@class='tt-dataset']//div[1]";
    WebElement firstElementDiv = driver.findElement(By.xpath(firstElementXPath)); //NoSuchElementException
    firstElementDiv.click();
}

尝试使用下面的xpath从动态列表中选择第一项

(//div[@id='fromStation-cg']//div[@class='tt-station tt-suggestion tt-selectable']//span)[1]
检查了下面的代码,该代码按预期工作

// fill first field
    WebElement textInput = driver.findElement(By.xpath("//input[@id='fromStation']"));
    textInput.sendKeys("Warszawa");
    Thread.sleep(5000);
    String firstElementXPath = "(//div[@id='fromStation-cg']//div[@class='tt-station tt-suggestion tt-selectable']//span)[1]";
    WebElement firstElementDiv = driver.findElement(By.xpath(firstElementXPath)); //NoSuchElementException
    wait.until(ExpectedConditions.elementToBeClickable(firstElementDiv));
    System.out.println(firstElementDiv.getText());
    firstElementDiv.click();

尝试使用下面的xpath从动态列表中选择第一项

(//div[@id='fromStation-cg']//div[@class='tt-station tt-suggestion tt-selectable']//span)[1]
检查了下面的代码,该代码按预期工作

// fill first field
    WebElement textInput = driver.findElement(By.xpath("//input[@id='fromStation']"));
    textInput.sendKeys("Warszawa");
    Thread.sleep(5000);
    String firstElementXPath = "(//div[@id='fromStation-cg']//div[@class='tt-station tt-suggestion tt-selectable']//span)[1]";
    WebElement firstElementDiv = driver.findElement(By.xpath(firstElementXPath)); //NoSuchElementException
    wait.until(ExpectedConditions.elementToBeClickable(firstElementDiv));
    System.out.println(firstElementDiv.getText());
    firstElementDiv.click();

代码的问题是您提供的xpath。没有
显示搜索后得到的任何建议的标签

要选择第一个建议,可以使用
findElements

WebElement textInput = driver.findElement(By.xpath("//input[@id='fromStation']"));
textInput.sendKeys("Warszawa");
List<WebElement> suggestionList = driver.findElements(By.xpath("//div[@class='tt-station tt-suggestion tt-selectable > span > i']"));
suggestion.get(0).click();
WebElement textInput=driver.findElement(By.xpath(“//input[@id='fromStation']);
textInput.sendKeys(“Warszawa”);
List suggestionList=driver.findelelements(By.xpath(“//div[@class='tt-station tt-suggestion tt-selectable>span>i']);
建议。获取(0)。单击();

如果要单击任何其他元素,可以根据需要提供索引号。

代码的问题是您提供的xpath。没有
显示搜索后得到的任何建议的标签

要选择第一个建议,可以使用
findElements

WebElement textInput = driver.findElement(By.xpath("//input[@id='fromStation']"));
textInput.sendKeys("Warszawa");
List<WebElement> suggestionList = driver.findElements(By.xpath("//div[@class='tt-station tt-suggestion tt-selectable > span > i']"));
suggestion.get(0).click();
WebElement textInput=driver.findElement(By.xpath(“//input[@id='fromStation']);
textInput.sendKeys(“Warszawa”);
List suggestionList=driver.findelelements(By.xpath(“//div[@class='tt-station tt-suggestion tt-selectable>span>i']);
建议。获取(0)。单击();
如果要单击任何其他元素,可以根据需要提供索引号