调整大小并打印下拉列表

时间:2018-10-29 13:35:14

标签: java selenium selenium-webdriver

我想在Amazons Departments下拉框中找到下拉框的长度。到目前为止,我已经掌握了下拉框长度的代码,但是只给出了数字1。我不知道如何打印出下拉框中其余的项目。到目前为止,这是我的代码:

        driver = new ChromeDriver();
    driver.get("https://www.amazon.com");

    Actions actions = new Actions(driver);
    WebElement ele = driver.findElement(By.xpath("//span[@class='nav-line-2' and contains(.,'Departments')]"));
    Thread.sleep(300);
    actions.moveToElement(ele);
    actions.perform();

    //Select dropDown = new Select (driver.findElement(By.xpath(".//*[@id='header']/div/div/div/nav/ul/li[4]/a")));
    List<WebElement> e = driver.findElements(By.xpath("//span[@class='nav-line-2' and contains(.,'Departments')]"));
    int itemsCount = e.size();
    System.out.println(itemsCount);
        for(WebElement element: e){
        System.out.println(ele.getText());
    }

1 个答案:

答案 0 :(得分:2)

您要再次定位Departments,但是项目位于另一个元素下

List<WebElement> elements = driver.findElements();

您还可以添加一些等待时间

WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@id='nav-flyout-shopAll']/div[contains(@class, 'nav-tpl-itemList')]/a"));