如何使用Selenium和Java从多选列表中获取所选选项的文本
我使用url中select类的selectByIndex()方法在“select”标记中选择多个选项[使用下面的代码]如何使用Selenium和Java从多选列表中获取所选选项的文本,java,selenium-webdriver,multi-select,Java,Selenium Webdriver,Multi Select,我使用url中select类的selectByIndex()方法在“select”标记中选择多个选项[使用下面的代码] //Selecting multiple options WebElement multiSelectElement = driver.findElement(By.id("multi-select")); Select select = new Select(multiSelectElement); select.selectByIndex(2); select.select
//Selecting multiple options
WebElement multiSelectElement = driver.findElement(By.id("multi-select"));
Select select = new Select(multiSelectElement);
select.selectByIndex(2);
select.selectByIndex(4);
List<String> selectedValues = new ArrayList<String>();
List<WebElement> selectedElements = select.getAllSelectedOptions();
for(WebElement element : selectedElements) {
selectedValues.add(element.getText());
}
//clicking on "Get All Selected"
driver.findElement(By.id("printAll")).click();
WebElement text = driver.findElement(By.xpath(""
+ "//button[@id='printAll']/following-sibling::p"));
//Getting the selected options
String textValue = text.getText();
//split the textValue storing the text after ":" into a variable
String[] s= textValue.split("are :");
System.out.println(s[1]);
//选择多个选项
WebElement multiSelectElement=driver.findElement(By.id(“multi-select”);
选择=新选择(多选元素);
select.selectByIndex(2);
select.selectByIndex(4);
List selectedValues=new ArrayList();
List selectedElements=select.getAllSelectedOptions();
for(WebElement元素:selectedElements){
selectedValues.add(element.getText());
}
//点击“全部选中”
driver.findElement(By.id(“printAll”))。单击();
WebElement text=driver.findElement(By.xpath(“)
+“//按钮[@id='printAll']/以下同级::p”);
//获取所选选项
字符串textValue=text.getText();
//将存储“:”之后文本的textValue拆分为变量
String[]s=textValue.split(“are:”);
System.out.println(s[1]);
代码应该打印所有选择的选项。但它只显示最后一个选择的选项。请更正。实际上您是通过
Get all selected
结果打印的,似乎应用程序有问题。但是您可以按如下方式提取字符串列表
:
List<String> selectedValues = new ArrayList<String>();
List<WebElement> selectedElements = select.getAllSelectedOptions();
for(WebElement element : selectedElements) {
selectedValues.add(element.getText());
}
for(String text: selectedValues) {
System.out.println(text);
}
List selectedValues=new ArrayList();
List selectedElements=select.getAllSelectedOptions();
for(WebElement元素:selectedElements){
selectedValues.add(element.getText());
}
用于(字符串文本:selectedValues){
System.out.println(文本);
}
事实上,您是通过获取所有选定的
结果打印的,看起来应用程序有问题。但是您可以按如下方式提取字符串列表
:
List<String> selectedValues = new ArrayList<String>();
List<WebElement> selectedElements = select.getAllSelectedOptions();
for(WebElement element : selectedElements) {
selectedValues.add(element.getText());
}
for(String text: selectedValues) {
System.out.println(text);
}
List selectedValues=new ArrayList();
List selectedElements=select.getAllSelectedOptions();
for(WebElement元素:selectedElements){
selectedValues.add(element.getText());
}
用于(字符串文本:selectedValues){
System.out.println(文本);
}
要提取多选列表中所选元素的文本,您需要将Encurve WebDriverWait与Actions
类一起用于元素定位()的可见性,您可以使用以下选项:
- 代码块:
public class A_demo
{
public static void main(String[] args) throws Exception
{
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");
((JavascriptExecutor)driver).executeScript("return arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[text()='Multi Select List Demo']"))));
WebElement california = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='California']")));
WebElement ohio = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='Ohio']")));
WebElement washington = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='Washington']")));
new Actions(driver).moveToElement(california).click().build().perform();
new Actions(driver).keyDown(Keys.CONTROL).click(ohio).keyUp(Keys.CONTROL).build().perform();
new Actions(driver).keyDown(Keys.CONTROL).click(washington).keyUp(Keys.CONTROL).build().perform();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='printAll' and @value='Print All']"))).click();
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='getall-selected']"))).getText());
driver.quit();
}
}
- 控制台输出:
Options selected are : California,Ohio,Washington
您可以在中找到基于Python的类似讨论
要提取多选列表中所选元素的文本,您需要将Encurve WebDriverWait与Actions
类一起用于ElementLocated()的可见性,您可以使用以下选项:
- 代码块:
public class A_demo
{
public static void main(String[] args) throws Exception
{
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");
((JavascriptExecutor)driver).executeScript("return arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[text()='Multi Select List Demo']"))));
WebElement california = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='California']")));
WebElement ohio = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='Ohio']")));
WebElement washington = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='Washington']")));
new Actions(driver).moveToElement(california).click().build().perform();
new Actions(driver).keyDown(Keys.CONTROL).click(ohio).keyUp(Keys.CONTROL).build().perform();
new Actions(driver).keyDown(Keys.CONTROL).click(washington).keyUp(Keys.CONTROL).build().perform();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='printAll' and @value='Print All']"))).click();
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='getall-selected']"))).getText());
driver.quit();
}
}
- 控制台输出:
Options selected are : California,Ohio,Washington
您可以在中找到基于Python的类似讨论
应用程序出现问题,因为在点击“全部选中”按钮后,它只显示最后一个选中的值。您的代码按预期工作。是的。当您手动执行时,它显示所有选中的值。在执行脚本时,只显示最后一个选中的值。我问了这个问题,以了解是否有任何错误g在我的代码中。是要打印所有选择的选项,还是要打印标签前面的文本选择的选项是:
?当您选择多个选项时,选择的选项将显示在那里。我要获取并显示它们。@debanjanb不要回滚我的编辑。这是Selenium WebDriver的问题,而不是Selenium question。OP没有询问任何有关XPath或WebDriverWait的问题…为什么要添加这些标记?它们与问题无关。应用程序有问题,因为它只在点击“获取所有选定项”按钮后显示最后选定的值。您的代码按预期工作。是的。当您手动执行。在执行脚本时,只显示最后选择的选项。我问了这个问题,想知道我的代码中是否有错误。是否要打印所有选择的选项,还是要打印标签前面的文本选择的选项是:
?当您选择多个选项时,所选选项将被显示放在那里。我想获取并显示它们。@debanjanb不要回滚我的编辑。这是一个Selenium WebDriver问题,不是Selenium问题。OP没有询问任何关于XPath或WebDriverWait的问题……为什么要添加这些标记?它们与问题无关。我已删除keyUp()以进行第二次单击和keyDown()对于第三次单击,脚本运行正常。可以这样做吗?@learningQA也应该可以。我刚刚演示了基本逻辑。是的。我这么认为。只是想确认一下。谢谢你。我已经实现了。我无法获得文本“所选选项是:”.我需要等待,上面的答案中提到了这一点。此外,答案还为我考虑了一些如何编写代码的问题。谢谢@DebanjanB。我已经删除了第二次单击的keyUp()和keyDown()对于第三次单击,脚本运行正常。可以这样做吗?@learningQA也应该可以。我刚刚演示了基本逻辑。是的。我这么认为。只是想确认一下。谢谢你。我已经实现了。我无法获得文本“所选选项是:”.我需要等待,上面的答案中提到了这一点。此外,答案还就如何编写代码向我提出了一些建议。谢谢@DebanjanB。