无法使用JavascrtptExecutor从下拉列表中选择选项
有人能给我提供一个故障保护(ish)方法来从我正在练习的页面的下拉列表中选择文本吗 具体来说,“从”和“到”机场下拉列表。我正在使用以下代码:无法使用JavascrtptExecutor从下拉列表中选择选项,java,selenium,webdriver,Java,Selenium,Webdriver,有人能给我提供一个故障保护(ish)方法来从我正在练习的页面的下拉列表中选择文本吗 具体来说,“从”和“到”机场下拉列表。我正在使用以下代码: public void selectWhereFrom(String query, String whereFromSelect) throws InterruptedException { WebElement dropDownContainer = driver.findElement(By.xpath(departureAirpor
public void selectWhereFrom(String query, String whereFromSelect) throws InterruptedException {
WebElement dropDownContainer = driver.findElement(By.xpath(departureAirportLocator));
dropDownContainer.click();
selectOption(query,whereFromSelect);
}
public void selectOption(String query, String option) {
String script =
"function selectOption(s) {\r\n" +
" var sel = document.querySelector(' " + query + "');\r\n" +
" for (var i = 0; i < sel.options.length; i++)\r\n" +
" {\r\n" +
" if (sel.options[i].text.indexOf(s) > -1)\r\n" +
" {\r\n" +
" sel.options[i].selected = true;\r\n" +
" break;\r\n" +
" }\r\n" +
" }\r\n" +
"}\r\n" +
"return selectOption('" + option + "');";
javaScriptExecutor(script);
}
public void selectWhereFrom(字符串查询,字符串whereFromSelect)引发中断异常{
WebElement dropDownContainer=driver.findElement(By.xpath(departureAirportLocator));
dropDownContainer.click();
selectOption(查询,whereFromSelect);
}
public void selectOption(字符串查询、字符串选项){
字符串脚本=
“函数选择选项{\r\n”+
var sel=document.querySelector(“+query+”);\r\n+
“对于(变量i=0;i-1)\r\n”+
“{\r\n”+
“sel.options[i]。selected=true;\r\n”+
“中断;\r\n”+
“}\r\n”+
“}\r\n”+
“}\r\n”+
“返回selectOption(““+option+”);”;
javaScriptExecutor(脚本);
}
这似乎成功地用文本填充了框,但当我点击“搜索”时,我收到一条消息,说我需要选择一个选项,表明它没有注册选择
我宁愿避免使用JavaScriptExecutor,但无法使这些选择与常规Selenium选择机制一起工作我会为每个下拉菜单设置一个函数,一个用于设置出发机场,另一个用于设置目的地机场。我已经测试了下面的代码,它可以工作 功能
public static void setDepartureAirport(String airport)
{
driver.findElement(By.cssSelector("div.departureAirport div.departurePoint")).click();
String xpath = "//div[contains(@class, 'departurePoint')]//ul//li[contains(@class, 'custom-select-option') and contains(text(), '"
+ airport + "')]";
driver.findElement(By.xpath(xpath)).click();
}
public static void setDestinationAirport(String airport)
{
driver.findElement(By.cssSelector("div.destinationAirport div.airportSelect")).click();
String xpath = "//div[contains(@class, 'destinationAirport')]//ul//li[contains(@class, 'custom-select-option') and contains(text(), '"
+ airport + "')]";
driver.findElement(By.xpath(xpath)).click();
}
你这样称呼他们
driver.get("https://www.club18-30.com/club18-30");
setDepartureAirport("(MAN)");
setDestinationAirport("(IBZ)");
我建议您使用三个字母的机场代码进行搜索,例如曼彻斯特的“MAN”。这对每个机场都是唯一的,但您可以使用文本的任何唯一部分。我会为每个下拉列表设置一个功能,一个用于设置出发机场,另一个用于设置目的地机场。我已经测试了下面的代码,它可以工作 功能
public static void setDepartureAirport(String airport)
{
driver.findElement(By.cssSelector("div.departureAirport div.departurePoint")).click();
String xpath = "//div[contains(@class, 'departurePoint')]//ul//li[contains(@class, 'custom-select-option') and contains(text(), '"
+ airport + "')]";
driver.findElement(By.xpath(xpath)).click();
}
public static void setDestinationAirport(String airport)
{
driver.findElement(By.cssSelector("div.destinationAirport div.airportSelect")).click();
String xpath = "//div[contains(@class, 'destinationAirport')]//ul//li[contains(@class, 'custom-select-option') and contains(text(), '"
+ airport + "')]";
driver.findElement(By.xpath(xpath)).click();
}
你这样称呼他们
driver.get("https://www.club18-30.com/club18-30");
setDepartureAirport("(MAN)");
setDestinationAirport("(IBZ)");
我建议您使用三个字母的机场代码进行搜索,例如曼彻斯特的“MAN”。这对每个机场都是唯一的,但您可以使用文本的任何唯一部分