如何使用selenium java从drowdown打印所选选项?

如何使用selenium java从drowdown打印所选选项?,java,selenium,selenium-webdriver,selenium-chromedriver,testng,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,Testng,从下拉列表中选择选项后,只需打印所选选项的值 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; publ

从下拉列表中选择选项后,只需打印所选选项的值

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());
            
        }
}

我得到了正确的结果,但我认为这不是两次编写选择类的最佳方式,因此您能否帮助我以更好的方式编写

在选择下拉元素后,您有正确的方法再次查找元素,但在选择后,您应该等待一段时间,使元素再次可见

请参阅以下代码:

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());
但在上面,我再次找到了元素,但没有为下拉列表创建新的变量名,仍然是
sortBy
。虽然在初始化时,新变量也应该起作用

不要忘记导入以下内容:

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

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

请参阅以下代码:

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());
但在上面,我再次找到了元素,但没有为下拉列表创建新的变量名,仍然是
sortBy
。虽然在初始化时,新变量也应该起作用

不要忘记导入以下内容:

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

首先,您必须等待元素可见。 其次,使用这个代码

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

首先,您必须等待元素可见。 其次,使用这个代码

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

删除
sortBy1
的声明,并使用
WebElement selected=sortBy.getFirstSelectedOption()
除非索引的设置导致元素
排序变为
陈旧
,在这种情况下,您的代码是正确的。是的,它引发了陈旧元素异常,这就是为什么我在代码正确时用另一个对象引用声明它的原因。不能使用同一对象,因为它已过时。但是,您可以使用相同的变量,但可以重新设置它的值,而不是使用
sortBy1
->
sortBy=new Select(driver.findElement(By.xpath(//Select[contains(@title,\'Sort By\”)[1])
删除
sortBy1
的声明,并使用
WebElement selected=sortBy.getFirstSelectedOption()
除非索引的设置导致元素
排序变为
陈旧
,在这种情况下,您的代码是正确的。是的,它引发了陈旧元素异常,这就是为什么我在代码正确时用另一个对象引用声明它的原因。不能使用同一对象,因为它已过时。但是,您可以使用相同的变量,但可以重新设置它的值,而不是使用
sortBy1
->
sortBy=new Select(driver.findElement(By.xpath(//Select[contains(@title,\'Sort By\”)[1])