gpt4 book ai didi

java - Selenium 中自动建议下拉菜单的点击值

转载 作者:行者123 更新时间:2023-11-30 02:30:09 25 4
gpt4 key购买 nike

我正在使用 java 在 selenium 中开发测试自动化。我怀疑我正在使用发送键发送自动建议下拉列表的值
locator.sendKeys("一些值");,
因此,当我使用
时,在下拉列表中会显示多个建议locator.sendKeys(Keys.ARROW_DOWN);
locator.sendKeys(Keys.Enter);
它始终选择最后一个选项。如何点击我发送的选项?

最佳答案

因为选择是 react Select ,以下功能将适合您。

在您的情况下,ID 是32

所以你可以这样调用它

reactSelect("32", "Male"); // reactSelect("32", "male");

功能

public void reactSelect(String id, String... values) {
By selectDropArrow = By.xpath("//div[@class='Select-control'][span[contains(@id,'react-select-" + id + "')]]/span[@class='Select-arrow-zone']");
WebElement dropDownArrow = driver.findElement(selectDropArrow);
dropDownArrow.click();
if (values != null) {
for (String value : values) {
WebElement option = driver.findElement(By.xpath("//div[@id='react-select-" + id + "--list']/div[@class='Select-option' and text()='" + value + "']"));
option.click();
}
}
}

完整示例

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/**
*
* @author Madhanraj
*/
public class SelTest {

WebDriver driver;

public SelTest() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

public void start() {
driver.get("http://jedwatson.github.io/react-select/");
reactSelect("3", "Caramel", "Peppermint");
}

public void reactSelect(String id, String... values) {
By selectDropArrow = By.xpath("//div[@class='Select-control'][span[contains(@id,'react-select-" + id + "')]]/span[@class='Select-arrow-zone']");
WebElement dropDownArrow = driver.findElement(selectDropArrow);
dropDownArrow.click();
if (values != null) {
for (String value : values) {
WebElement option = driver.findElement(By.xpath("//div[@id='react-select-" + id + "--list']/div[@class='Select-option' and text()='" + value + "']"));
option.click();
}
}
}

public void quit() {
driver.quit();
}

public static void main(String[] args) {
SelTest ss = new SelTest();
ss.start();
ss.quit();
}

}

关于java - Selenium 中自动建议下拉菜单的点击值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44499967/

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