简体   繁体   中英

I am unable to get xpath for walmart homepage dropdown box using selenium webdriver in Java

I am trying to code in java using selenium webdriver to click on Dropdown list in walmart page. But I am unable to access the li elements.

<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>

I wanted to access Baby using selenium webdriver in Java. Below is my code:

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());           
            }

But this gives an error as below:-

"Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 132 milliseconds".

Please help

You defined WebDriverWait but you don't use it

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("dropdown")));

Implicit wait will wait for element to exist in the DOM. If you want to use explicit wait you need to use Expected Conditions as well. Only defining WebDriverWait doesn't actually do anything.

Your code looks fine. Please try below xpath :-

//div[@class='js-flyout-modal flyout-modal']//ul[@class='block-list']/li

Now first try to put thread.sleep if it works then it is problem of wait only

Thread.sleep(30000);

But then don't use Thread for your script as it's not recommended .. It's just to ensure that script is failing because of time

Hope it will help you :)

沃尔玛页面中的其他元素可能与您的xpath //*[@class='block-list']/li匹配,并且该元素不可见。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM