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

标签 java selenium-webdriver drop-down-menu webdriver webdriverwait

正在 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/

相关文章:

java - 使用 AffineTransform 将形状缩放/平移到给定的矩形

java - 如何知道在 Java 中使用进程构建器/运行时调用的进程是否已完成执行

python - Selenium webdriver python找不到按钮

css - 我的 CSS Mega 菜单从下拉选项扩展到右侧而不是左侧?

java - 使用 Selenium WebAPI 进行 Jira 测试自动化

java - 如何在java中添加 protected 变量

html - 基于文本的跨度的 XPath?

java - 无法单击网站上的少数链接,其余其他链接可以使用 Selenium 单击

html - 选择标签的设置宽度不起作用

javascript - 仅停止一个下拉切换关闭点击