gpt4 book ai didi

java - 调整并打印下拉项目列表

转载 作者:行者123 更新时间:2023-12-02 01:50:31 25 4
gpt4 key购买 nike

我想找到亚马逊部门下拉框中下拉框的长度。到目前为止,我已经有了下拉框长度的代码,但它只给出了数字 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());
}

最佳答案

您再次找到部门,但这些项目位于另一个元素下

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"));

关于java - 调整并打印下拉项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53046654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com