我是自动化测试新手,现在我正在尝试在下拉菜单中选择值。据我了解,我的示例中有两个下拉菜单,但缺乏经验使得很难弄清楚如何解决这个问题。我现在正在处理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/