使用selenium给出错误来获取java中的隐藏元素

时间:2015-12-03 11:30:56

标签: java selenium selenium-webdriver

当我选择隐藏其显示错误的值为:

  

线程中的异常" main" org.openqa.selenium.NoSuchElementException:   无法找到元素:{"方法":"部分链接   文本""选择器":"车辆使"}

这是我的代码:

package section5.advWays.locatingObjects;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;   

public class CusXPathUsingAtt1 {
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver wd = new FirefoxDriver();
        wd.manage().window().maximize();
        Thread.sleep(5000);         wd.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
        Select SelectMakedropdown = new Select(wd.findElement(By.id("vehicle-make")));      
        SelectMakedropdown.selectByVisibleText("BMW");
        Select YearSelectDropdown = new Select(wd.findElement(By.id("vehicle-year")));
        YearSelectDropdown.selectByVisibleText("2011");
        Select VehicleSelectDropdown = new Select(wd.findElement(By.id("vehicle-model")));
        VehicleSelectDropdown.selectByVisibleText("228i xDrive Coupe");
    }
    }

如何使用selenium webdriver选择那些下拉列表?

2 个答案:

答案 0 :(得分:1)

有两件事:

  1. 我发现您首先需要点击一个元素,否则Select菜单将无法打开。因此,在我的代码中,我首先点击元素以启用选择菜单。
  2. 还有一些元素不易获得。比如说,除非输入Make,否则年份不会启用。
  3. 请参阅以下代码:

    WebDriver driver= new FirefoxDriver();
    driver.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
    WebDriverWait wait = new WebDriverWait(driver, 30);
    
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Make')]")));
    driver.findElement(By.xpath("//div[contains(text(),'Select Make')]")).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-make")));
    Select SelectMakedropdown = new Select(driver.findElement(By.id("vehicle-make")));      
    SelectMakedropdown.selectByVisibleText("BMW");
    
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Year')]")));
    driver.findElement(By.xpath("//div[contains(text(),'Select Year')]")).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-year")));
    Select YearSelectDropdown = new Select(driver.findElement(By.id("vehicle-year")));
    YearSelectDropdown.selectByVisibleText("2011");
    
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Model')]")));
    driver.findElement(By.xpath("//div[contains(text(),'Select Model')]")).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-model")));
    Select VehicleSelectDropdown = new Select(driver.findElement(By.id("vehicle-model")));
    VehicleSelectDropdown.selectByVisibleText("128i Cabriolet Base Model");
    
    driver.quit();
    

    Firefox更新:

    我尝试了很多,但我仍然无法确定为什么选择在Firefox中运行。但我仍然设法解决这个问题。在这里,我使用较少的点击次数和更多应用支持的功能。

    WebDriver driver= new FirefoxDriver();
    driver.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
    WebDriverWait wait = new WebDriverWait(driver, 30);
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Make')]")));
    WebElement we1 = driver.findElement(By.xpath("//div[contains(text(),'Select Make')]"));
    executor.executeScript("arguments[0].click();", we1);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-make")));
    WebElement SelectMakedropdown = driver.findElement(By.id("vehicle-make"));      
    SelectMakedropdown.sendKeys("BMW");
    SelectMakedropdown.sendKeys(Keys.ENTER);
    Thread.sleep(1000);
    
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Year')]")));
    WebElement we2 = driver.findElement(By.xpath("//div[contains(text(),'Select Year')]"));
    executor.executeScript("arguments[0].click();", we2);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-year")));
    WebElement YearSelectDropdown = driver.findElement(By.id("vehicle-year"));
    YearSelectDropdown.sendKeys("2011");
    YearSelectDropdown.sendKeys(Keys.ENTER);
    Thread.sleep(1000);
    
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Model')]")));
    WebElement we3 = driver.findElement(By.xpath("//div[contains(text(),'Select Model')]"));
    executor.executeScript("arguments[0].click();", we3);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-model")));
    WebElement VehicleSelectDropdown = driver.findElement(By.id("vehicle-model"));
    VehicleSelectDropdown.sendKeys("128i Cabriolet Base Model");
    VehicleSelectDropdown.sendKeys(Keys.ENTER);
    

答案 1 :(得分:0)

看来页面首先要点击下拉列表,试试这段代码:

TextBox

上面肯定会跑。