【发布时间】:2019-07-07 21:28:33
【问题描述】:
我在从动态生成的下拉列表中指定第一个元素的 xpath 时遇到问题。我希望 Selenium 在输入一些文本后单击this webpage 下拉列表中的第一个建议。但是,我想要找到它的方式是NoSuchElementException。我的代码:
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();
}
【问题讨论】:
标签: java selenium web-crawler selenium-chromedriver