gpt4 book ai didi

java - Selenium 下拉选择不起作用

转载 作者:行者123 更新时间:2023-12-02 03:00:56 26 4
gpt4 key购买 nike

通过 Eclipse IDE 使用 Java 测试 Firefox。下面是我尝试测试的网站的 HTML 代码:

<td class="selectCommandHolder">
<select id="MainContent_DropDownQueryField" name="ctl00$MainContent$DropDownQueryField" aria-invalid="false">
<option value="ICAO" selected="selected"> ICAO </option>
<option value="IATA"> IATA </option>
<option value="Airfields.Name"> Name </option>
<option value="City"> City </option>
<option value="States.Name"> State </option>
</select>
</td>

默认选项是第一个“ICAO”。尝试让我的测试通过值选择第二个“IATA”。Selenium java 代码如下:

    WebElement searchOption = driver.findElement(By.id("MainContent_DropDownQueryField")); 

Select searchIATA = new Select(searchOption);

WebElement selected_value = searchIATA.getFirstSelectedOption();
System.out.println("Selected values is "+ selected_value.getText());

Thread.sleep(1000);
searchIATA.selectByValue("IATA");

if (searchIATA.getFirstSelectedOption().getText().trim().equals("IATA")){
System.out.println("succesfully selected IATA.");
} else{
System.out.println("selected value is not IATA, it is:" + selected_value.getText());
}

出于某种原因,我无法选择 IATA,尽管我相信我正在正确地尝试以它为目标。我的控制台输出如下所示:

1487691940734   Marionette  INFO    sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691940749 Marionette INFO sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691940760 Marionette INFO sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691940769 Marionette INFO sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691940777 Marionette INFO sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
Selected values is ICAO
1487691941789 Marionette INFO sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691941795 Marionette INFO sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691941801 Marionette INFO sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691941822 Marionette INFO sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
1487691941829 Marionette INFO sendAsync 162acaca-70ac-8e4f-96ff-1b3bd1a5c1ef
Selected values is not IATA, it is: ICAO

我可以打印“allSelectedOptions”,它显示上面的“选项值”列表。我在这里缺少什么?

最佳答案

我认为您的代码中有一些错误,您正在检查 if condition 中的旧值此外,您的下拉值中存在空格,因此它与您的 string 不匹配

按如下方式更改您的代码并尝试 -

searchIATA.selectByValue("IATA");
if (searchIATA.getFirstSelectedOption().getText().trim().equals("IATA"))
{
System.out.println("Succesfully selected IATA.");
}
else
{
System.out.println("Selected values is not IATA, it is: "+ selected_value.getText());
}

更新了完整代码

    WebElement searchOption = driver.findElement(By.id("MainContent_DropDownQueryField")); 

Select searchIATA = new Select(searchOption);

WebElement selected_value = searchIATA.getFirstSelectedOption();
System.out.println("Selected values is "+ selected_value.getText());

Thread.sleep(2500);
searchIATA.selectByValue("IATA");
Thread.sleep(2500);
System.out.println(searchIATA.getFirstSelectedOption().getText());

if (searchIATA.getFirstSelectedOption().getText().trim().equals("IATA"))
{
System.out.println("Succesfully selected IATA.");
}
else
{
System.out.println("Selected values is not IATA, it is: "+ selected_value.getText());
}
}

关于java - Selenium 下拉选择不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42371924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com