我无法使用Java中的SeleniumWebDriver获取沃尔玛主页下拉框的xpath
我试图用java编写代码,使用SeleniumWebDriver单击沃尔玛页面中的下拉列表。但是我无法访问li元素我无法使用Java中的SeleniumWebDriver获取沃尔玛主页下拉框的xpath,java,selenium,xpath,selenium-webdriver,webdriver,Java,Selenium,Xpath,Selenium Webdriver,Webdriver,我试图用java编写代码,使用SeleniumWebDriver单击沃尔玛页面中的下拉列表。但是我无法访问li元素 <button class="js-flyout-toggle dropdown" aria-haspopup="true" type="button" data-cat-id="0" aria-expanded="false"> All </button> <div class="js-flyout-modal flyout-modal"> &
<button class="js-flyout-toggle dropdown" aria-haspopup="true" type="button" data-cat-id="0" aria-expanded="false"> All </button>
<div class="js-flyout-modal flyout-modal">
<ul class="block-list">
<li><button class="no-margin font-semibold" type="button" data-cat-id="0" tabindex="-1"> All Departments </button></li>
<li><button class="no-margin font-semibold" type="button" data-cat-id="91083" tabindex="-1"> Auto & Tires </button></li>
<li><button class="no-margin font-semibold" type="button" data-cat-id="5427" tabindex="-1"> Baby </button></li>
全部
- 各部门
- 汽车与轮胎
- 宝贝
我想在Java中使用SeleniumWebDriver访问Baby。
下面是我的代码:
driver.get("http://www.walmart.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = driver.findElement(By.xpath("html/body/div[2]/header/div[3]/div/div/div/div/div[3]/form/div/div[1]/div/button"));
dropdown.click();
List<WebElement> liElements = driver.findElements(By.xpath("//*[@class='block-list']/li"));
for ( WebElement we: liElements) {
System.out.println(we.getText());
}
driver.get(“http://www.walmart.com");
WebDriverWait wait=新的WebDriverWait(驱动程序,10);
WebElement dropdown=driver.findElement(By.xpath(“html/body/div[2]/header/div[3]/div/div/div/div[3]/form/div/div[1]/div/button”);
下拉列表。单击();
列表元素=driver.findElements(By.xpath(“/*[@class='block-List']/li”);
对于(WebElement we:liElements){
System.out.println(we.getText());
}
但这会产生如下错误:-
线程“main”中出现异常
org.openqa.selenium.ElementNotVisibleException:元素不是
当前可见,因此可能无法与命令持续时间交互
或超时:132毫秒”
请帮助您定义了
WebDriverWait
,但您没有使用它
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("dropdown")));
隐式等待将等待元素在DOM中存在。如果要使用显式等待,还需要使用。仅定义
WebDriverWait
实际上没有任何作用。您的代码看起来很好。请尝试以下xpath:-
//div[@class='js-flyout-modal flyout-modal']//ul[@class='block-list']/li
现在,首先尝试放置thread.sleep,若它有效,那个么这只是等待的问题
Thread.sleep(30000);
但是,不要在脚本中使用线程,因为这是不推荐的。。这只是为了确保脚本由于时间而失败
希望它能帮助您:)可能是沃尔玛页面中的其他元素与您的xpath匹配
/*[@class='block-list']/li
并且该元素不可见。尝试以下操作:driver.manage().timeout().implicitlyWait(10,TimeUnit.SECONDS)代码>。查看官方文档谢谢您将尝试此感谢您提供的信息,但我不明白您为什么使用类名作为“下拉列表”,这样的类名在代码中不存在?@user3714889是的,是的。看看
有两个类:js弹出式切换
和下拉
谢谢,我现在得到了它,但是代码运行良好,直到:WebElement dropdown=driver.findElement(By.xpath(“html/body/div[2]/header/div[3]/div/div/div[3]/form/div/div/div[1]/div/button”);下拉列表。单击();。问题源于List liElements=driver.findElements(By.xpath(“//*[@class='block-List']/li”);对于(WebElement we:liElements){System.out.println(we.getText());}@user3714889有什么问题?我无法访问li标记值