如何使用Selenium Java打印下拉菜单中的已选择选项?

huangapple go评论117阅读模式
英文:

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

问题

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);
        WebElement selected = sortBy.getFirstSelectedOption();

        System.out.println(selected.getText());

        driver.quit();
    }
}

Please note that I've made the code formatting improvements and added the driver.quit() method at the end to properly close the WebDriver instance.

英文:

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

答案1

得分: 0

你在选择下拉元素后再次查找元素的方式是正确的,但在选择后,你应该等待一段时间以使元素再次可见。

请参考以下代码:

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

// 在这里等待
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());

但是在上述代码中,我再次查找元素时并没有为下拉框创建新的变量名称,仍然是 sortBy。尽管使用新的变量名称初始化也应该能正常工作。

不要忘记导入以下内容:

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

参考讨论链接

英文:

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.

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. 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

答案2

得分: 0

首先,您必须等待元素可见。
其次,使用以下代码:

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

Firstly, you have to wait element visible.
Secondly, use this code

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

huangapple
  • 本文由 发表于 2020年9月28日 21:55:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64103630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定