gpt4 book ai didi

java - 如何使用 Selenium Java 处理 https ://www. SPACEJET.com/PASSENGERS 字段的静态下拉列表

转载 作者:行者123 更新时间:2023-12-01 19:12:57 25 4
gpt4 key购买 nike

正在 Spice jet 网站上练习自动化。这里给出我编写的代码。

package NewPackage;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class HandlingStaticDropdowns
{
public static void main(String[] args)throws Exception
{
System.setProperty("webdriver.chrome.driver","C:\\SeleniumJars\\chromedriver.exe");
WebDriverdriver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get("https://www.spicejet.com/");
Select s = newSelect(driver.findElement(By.id("ctl00_mainContent_ddl_Adult")));
s.selectByValue("3");
s.selectByIndex(6);
s.deselectByVisibleText("5");
}
}

我没有得到正确的输出。请帮助我并让我知道我做错了什么。

最佳答案

PASSENGERS 字段包含 3(三) ,一位为成人人数,一位为 child 人数,另一位为婴儿人数。要为成人选择5,为 child 选择3,为选择1婴儿,您需要为visibilityOfElementLocated()诱导WebDriverWait,并且可以使用以下Locator Strategies :

  • 代码块:

    public class A_demo 
    {
    public static void main(String[] args) throws Exception
    {
    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.setExperimentalOption("useAutomationExtension", false);
    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://www.spicejet.com/");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#divpaxinfo"))).click();
    Select selectAdult = new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#divpaxOptions div.passengers-wrap2 p>select[name$='_Adult']"))));
    selectAdult.selectByVisibleText("5");
    Select selectChild = new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#divpaxOptions div.passengers-wrap2 p>select[name$='_Child']"))));
    selectChild.selectByVisibleText("3");
    Select selectInfant = new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#divpaxOptions div.passengers-wrap2 p>select[name$='_Infant']"))));
    selectInfant.selectByVisibleText("1");
    }
    }
  • 浏览器快照:

spicejet_passengers

关于java - 如何使用 Selenium Java 处理 https ://www. SPACEJET.com/PASSENGERS 字段的静态下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59453704/

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