无法使用WebDriver从下拉列表的选项中获取文本值

时间:2014-04-22 10:52:35

标签: java selenium webdriver

import java.util.List;
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;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class DellDropdown {

    public static void main(String[] args)
    {
        WebDriver driver=new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        WebDriverWait wait = new WebDriverWait(driver, 30);
        driver.get("http://www.dell.com/");
        driver.findElement(By.xpath("//a[@class='ctryName']")).click();
        WebElement countryDropdown=driver.findElement(By.xpath("//select[@class='para_small']"));
        Select selectElement = new Select(countryDropdown);
        selectElement.selectByVisibleText("India");
        if(driver.findElement(By.xpath("//a[@class='ctryName' and text()='India']")).getText().equals("India")){
            System.out.println("The dropdown is changed to india");
        }
        WebElement selectedCountryDropdown=driver.findElement(By.xpath("//select[@class='para_small']"));
        selectElement = new Select(selectedCountryDropdown);
        List<WebElement> options=selectElement.getOptions();
        for(int i=0;i<options.size();i++){
            WebElement option=options.get(i);
            String countryName=option.getText();
            boolean selectedValue=option.isSelected();
            System.out.print(countryName);
            System.out.println(selectedValue);
        }
        driver.close();
    }
}

从戴尔网站的国​​家/地区下拉菜单中选择国家印度 我没有从下拉列表的选项标签中获取文本,只返回选定的状态。请告诉我如何获取文本?

1 个答案:

答案 0 :(得分:1)

编辑:重写我的回答以使其更清晰

问题是,当选择下拉列表关闭时,选择下拉列表中的选项不包含任何文本。您只能看到选择了哪个选项:

<select class="para_small" name="" size="20" data-index="75">
<option value="/aa/en/gen/df.aspx?refid=df&s=gen&~ck=cr"></option>
<option value="/al/en/gen/df.aspx?refid=df&s=gen&~ck=cr"></option>
<option value="/dz/fr/gen/df.aspx?refid=df&s=gen&~ck=cr"></option>
...
<option value="/in/en/gen/df.aspx?refid=df&s=gen" selected="selected"></option>
...
</select>

您需要做的是在选择&#34; India&#34;之后再次点击并打开该下拉列表。并重新加载页面。

这是你可以做到的:

// the fix
driver.findElement(By.xpath("//a[@class='ctryName']")).click();
// end of fix

这是我的完整代码,我的修复程序会告诉你它在哪里完成:

import java.util.List;
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;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class DellDropdown {

    public static void main(String[] args)
    {
        WebDriver driver=new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        WebDriverWait wait = new WebDriverWait(driver, 30);
        driver.get("http://www.dell.com/");
        driver.findElement(By.xpath("//a[@class='ctryName']")).click();
        WebElement countryDropdown=driver.findElement(By.xpath("//select[@class='para_small']"));
        Select selectElement = new Select(countryDropdown);
        selectElement.selectByVisibleText("India");
        if(driver.findElement(By.xpath("//a[@class='ctryName' and text()='India']")).getText().equals("India")){
            System.out.println("The dropdown is changed to india");
        }

        // the fix
        driver.findElement(By.xpath("//a[@class='ctryName']")).click();
        // end of fix

        WebElement selectedCountryDropdown=driver.findElement(By.xpath("//select[@class='para_small']"));
        selectElement = new Select(selectedCountryDropdown);
        List<WebElement> options=selectElement.getOptions();
        for(int i=0;i<options.size();i++){
            WebElement option=options.get(i);
            String countryName=option.getText();
            boolean selectedValue=option.isSelected();
            System.out.print(countryName);
            System.out.println(selectedValue);
        }
        driver.close();
    }
}

注意:确保在测试运行期间不会干扰浏览器,否则鼠标移动将生成onmouseout事件,下拉菜单将关闭,文本值将消失!

编辑:要确保选择下拉列表保持打开状态,您可以在选项循环的每次迭代中将鼠标移动到该下拉列表。但是,该解决方案会减慢代码速度,因此您必须选择最适合您的代码。

将鼠标移动到&#34; for&#34;循环:

    for(int i=0;i<options.size();i++){
        //fix
        Actions mouseAction = new Actions(driver);
        mouseAction.moveToElement(selectedCountryDropdown).build().perform();
        //end of fix

        WebElement option=options.get(i);
        String countryName=option.getText();
        boolean selectedValue=option.isSelected();
        System.out.print(countryName);
        System.out.println(selectedValue);
    }