selenium 在makemytrip网站下选择下拉元素时出现问题

bjg7j2ky  于 2022-12-26  发布在  其他
关注(0)|答案(2)|浏览(143)

在以下网站下选择下拉元素时出现问题
http://flights.makemytrip.com/makemytrip/fareCal.do?intid=NewHP_to_DF_FC_Menu
我无法选择下面列出的任何一个城市。请帮助我解决相同的问题。

尝试的方案

driver.findElement(By.className("chzn-single")).click();

driver.findElement(By.xpath("//span[contains,'NewDelhi']")).click();

driver.findElement(By.xpath("//span[@id='fromcity_chzn']")).click();
cnwbcb6i

cnwbcb6i1#

这是可行的:

WebElement leavingFrom = driver.findElement(By.xpath("//*[@id='fromcity_chzn']/a"));
    WebElement goingTo = driver.findElement(By.xpath("//*[@id='tocity_chzn']/a"));

    leavingFrom.click();
    leavingFrom.sendKeys("Bangalore");
    leavingFrom.sendKeys(Keys.RETURN);

    goingTo.click();
    goingTo.sendKeys("Goa");
    goingTo.sendKeys(Keys.RETURN);
hec6srdp

hec6srdp2#

以下是工作示例:

//First get main dropDowns
var leavingFromDropDown = driver.FindElement(By.Css("#fromcity_chzn"));
var goingToDropDown = driver.FindElement(By.Css("#tocity_chzn"));

//Select value from first dropDown using dropDown items index
//First click on dropDown to open it
leavingFromDropDown.click();
//Now find items in it and click on any item using it's index (also can be used method to access this elements using their names
leavingFromDropDown.FindElements(By.Css(".active-result"))[1].click();
//this dropDown closes automatically, but if not you need to click on it again to close

//Same perform with second element
goingToDropDown.click();
goingToDropDown.FindElements(By.Css(".active-result"))[2].click();

如果您想使用输入框输入下拉列表中的任何值,您需要找到该元素并使用sendKeys设置其值。

leavingFromDropDown.click();
var input = leavingFromDropDown.FindElement(By.Css(".chzn-search > input"));
input.sendKeys('Goa');
input.sendKeys(Keys.Enter);//or tab

相关问题