简体   繁体   English

如何使用 selenium java 从下拉列表中打印选定的选项?

[英]How to print selected option from drowdown by using selenium java?

just want to print the value of selected option after selecting the option from dropdown .从下拉列表中选择选项后只想打印所选选项的值。

package Webbasics;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class ecommerce {
    public static void main(String args[]) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","C:\\Program Files\\selenium\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
            driver.get("http://live.demoguru99.com/index.php/");
            driver.manage().window().maximize();
            driver.findElement(By.xpath("//*[@id=\"nav\"]//li[1]/a")).click();
            
            Select sortBy=new Select(driver.findElement(By.xpath("(//select[contains(@title,\"Sort By\")])[1]")));
            
            sortBy.selectByIndex(1);
            Select sortBy1=new Select(driver.findElement(By.xpath("(//select[contains(@title,\"Sort By\")])[1]")));
            WebElement selected=sortBy1.getFirstSelectedOption();
            
            System.out.println(selected.getText());
            
        }
}

I am getting proper result but i think its not the best way of writing select class two times so can you help me to write in a better way我得到了正确的结果,但我认为这不是写两次选择类的最佳方式,所以你能帮我写得更好吗

You have correct way with find element again after select the dropdown element, but after selected you should wait a while to make the element visible again.选择下拉元素后,您再次使用 find 元素的正确方法,但选择后您应该等待一段时间以使该元素再次可见。

See the following code:请参阅以下代码:

Select sortBy = new Select(driver.findElement(By.xpath("(//select[contains(@title,'Sort By')])[1]")));
sortBy.selectByIndex(1);

//wait here
WebDriverWait wait = new WebDriverWait(driver, 20);
sortBy = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//select[contains(@title,'Sort By')])[1]"))));
System.out.println(sortBy.getFirstSelectedOption().getText());

But above I find again the element without creating a new variable name for the dropdown, still sortBy .但是在上面我再次找到了没有为下拉列表创建新变量名的元素,仍然是sortBy Although with initialization the new variables should work too.尽管通过初始化,新变量也应该起作用。

Don't forget to import the following:不要忘记导入以下内容:

import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

Reference discussion 参考讨论

Firstly, you have to wait element visible.首先,您必须等待元素可见。 Secondly, use this code其次,使用此代码

Select select = new Select("Element");
WebElement tmp = select.getFirstSelectedOption();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium WebDriver 和 Java 获取选定的选项 - How to get selected option using Selenium WebDriver with Java 使用带有Java的Selenium WebDriver获取选择的选项 - Get selected option using Selenium WebDriver with Java 如何获取所选下拉列表的文本值 - how to get the text value of the selected drowdown 如何打印通过硒中的“ selectByVisibleText”方法选择的所选选项的文本 - How to print the text of the selected option choosen through 'selectByVisibleText' method in selenium 如何使用Selenium和Java从自动建议中选择一个选项? - How to select an option from auto suggestions using Selenium and Java? 如何使用 selenium webdriver 和 Java 从剑道下拉菜单中选择一个选项 - How to select an option from the kendo dropdown using selenium webdriver and Java 如何使用 Selenium 和 Java 识别是否选择了带有伪元素::after 样式的单选按钮选项 - How to identify if a radio button option styled with pseudo element ::after is selected or not using Selenium and Java 如何使用 Selenium 从下拉列表中选择一个选项 select - How to select an option from a dropdown using Selenium 当元素不是选择类型输入时,如何使用Selenium Webdriver Java从下拉列表中获取所有选项值? - how to get all option values from dropdown using selenium webdriver java when the element is not a select type input? 如何使用带有Java的Selenium WebDriver从下拉列表中选择一个选项? - How to select an option from a drop-down using Selenium WebDriver with Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM