java - 我如何需要在下拉菜单中正确选择项目?

标签 java selenium automation automated-tests selenium-chromedriver

我是自动化测试新手,现在我正在尝试在下拉菜单中选择值。据我了解,我的示例中有两个下拉菜单,但缺乏经验使得很难弄清楚如何解决这个问题。我现在正在处理https://www.spicejet.com/我想做的是选择乘客,然后单击成人并设置应该有多少成人。

我看过一些如何选择下拉菜单的视频,很少有人建议使用简单的驱动程序并使用其他点击来创建选择对象并使用它。由于错误,没有写太多代码。另外,据我了解,我对“选择”感到迷失,我创建了新对象,将驱动程序对象传递给他并执行操作?

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 dropdown {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.spicejet.com/"); // URL in the browser

        driver.manage().window().maximize(); // Maximize the browser

        Select s = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_originStation1")));
        s.selectByValue("2");
    }
}

这个有效 ->

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 dropdown {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.spicejet.com/"); // URL in the browser

        driver.manage().window().maximize(); // Maximize the browser

        // Get specific area to save it as variable and check it later if we are in right web page
        String verifyPage = driver.findElement(By.xpath("//span[contains(text(),'Flights')]")).getText();

        // Check it with IF
        if (verifyPage.contentEquals("Flights")) {
            System.out.println("[1] You are IN the right page.");
        } else {
            System.out.println("[2] You are NOT in the right page.");
        }

        driver.findElement(By.xpath("//div[@id='divpaxinfo']")).click();
        Select dropdown = new Select(driver.findElement(By.xpath("//select[@id='ctl00_mainContent_ddl_Adult']")));
        dropdown.selectByIndex(1);
    }
}

最佳答案

s.selectByValue("AMD");

选择/取消选择“value”属性与指定参数匹配的选项。 更正了您的代码。希望有所帮助。如果没有,请粘贴错误消息

关于java - 我如何需要在下拉菜单中正确选择项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58684292/

相关文章:

java - 如何在java程序中调用shell脚本?

java - 在 jmeter 中获取错误 "Response code: Non HTTP response code: org.apache.http.conn.HttpHostConnectException"

c++ - 以编程方式在 IE 中设置页面缩放和文本大小

macos - 视觉识别鼠标单击自动化实用程序?

java - Android Parcelable - 使用通用数据类型将数据读/写到 Parcel

java - 通过图像透明查看JButton Java

java - 使用 Web 界面或任何可视化工具运行 Selenium WebDriver 测试

Python Selenium Webdriver - 查找嵌套框架

java - 代码未将密码值粘贴到网页的密码字段中

delphi - 如何将IDL导入到Delphi中?