本文介绍了如何使用Selenium从动态下拉列表中选择一个选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图单击下拉值以从使我的旅程 http://www的字段中选择城市. makemytrip.com/.但是获取Stale元素引用异常.网页加载中的ID发生了变化.尝试以下代码:

I am trying to click on dropdown value to select city in from field in Make my trip http://www.makemytrip.com/. But getting Stale element reference exception. Ids are getting changed on page load.Tried below code:

driver.findElement(By.xpath(".//*[@id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[@id='ui-id-1']"));
driver.findElement(By.xpath(".//*[@id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeSelected(driver.findElement(By.xpath(".//*[@class='ui-menu-item'][2]"))));

推荐答案

要点击一个下拉值,例如孟买,您可以使用以下解决方案:

To click on a dropdown value e.g. Mumbai you can use the following solution:

  • 代码块:

  • Code Block:

driver.get("https://www.makemytrip.com/")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='input_fromto checkSpecialCharacters ui-autocomplete-input' and @id='hp-widget__sfrom']"))).click();
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//li[@class='ui-menu-item'][starts-with(@id,'ui-id-')]//span[@class='autoCompleteItem__label']")));
for (WebElement element:myList)
    if(element.getText().contains("Mumbai"));
        element.click();

  • 浏览器快照:

  • Browser Snapshot:

    这篇关于如何使用Selenium从动态下拉列表中选择一个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-26 04:49