gpt4 book ai didi

java - 无法在下拉框中找到 Web 元素?

转载 作者:行者123 更新时间:2023-11-30 10:27:52 24 4
gpt4 key购买 nike

我无法在下拉框中找到特定元素。如何定位性别类型“男”或“女”?

代码:

Reporter.log("=====Browser Session Started=====", true);
System.setProperty("webdriver.chrome.driver","D:\\Java Selenium\\Selenium jar\\driver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();

driver.manage().window().maximize();
driver.get("http://www.delta.com");
Reporter.log("=====Application Started=====", true);

//Click SIGN UP to enter Personnel information for creating Delta login//

driver.findElement(By.linkText("SIGN UP")).click();
Thread.sleep(500);

//Enter Personnel information for creating Delta login//
//To select the Prefix using drop down via ID locator
driver.findElement(By.cssSelector("#basicInfoTitle-button")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Select the option from drop down
driver.findElement(By.cssSelector("#ui-id-28")).click();
Thread.sleep(500);

driver.findElement(By.cssSelector("#basicInfoFirstName")).sendKeys("Suresh");
Thread.sleep(500);
driver.findElement(By.cssSelector("#basicInfoMiddleName")).sendKeys("Ram");
Thread.sleep(500);
driver.findElement(By.cssSelector("#basicInfoLastName")).sendKeys("Ponrajan");
Thread.sleep(500);

//To select the Gender using drop down via ID locator
driver.findElement(By.cssSelector("#basicInfoGender-button")).click();
//Select the option from drop down
Actions action = new Actions(driver);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
action.moveToElement(driver.findElement(By.xpath("//*[@id='ui-id-40']")));
action.click();

最佳答案

您需要使用“选择”定位器。

Select theDropdown = driver.findElement(By.cssSelector("#basicInfoGender-button"));

theDropdown.selectByVisibleText("Male");

theDropdown.selectByVisibleText("Female");

您也可以通过索引选择它。

theDropdown.selectByIndex(1);

关于java - 无法在下拉框中找到 Web 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062445/

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