2

This is the code of the demo store I want to test:

 <li class="level0 nav-2 parent">
      <a href="http://demo-store.seleniumacademy.com/men.html" 
               class="level0 has-children">Men</a> 
        <ul class="level0">
            <li class="level1 view-all">
               <a class="level1" href="http://demo- 
                         store.seleniumacademy.com/men.html">View All 
                         Men</a>
            </li>
               <li class="level1 nav-2-1 first"><a></a></li>
               <li class="level1 nav-2-2"><a href="http://demo- 
                          store.seleniumacademy.com/men/shirts.html" 
                          class="level1">text to get</a> 
               </li>
               <li class="level1 nav-2-3"><a></a></li>
         </ul>
    </li>

I want to get text of those subcategories so I can later click on specific category by using text inside of a link element. My code is:

public Subcategory openSubcategory (String subcategoryName){

    List<WebElement> subcategories = driver.findElements(By.cssSelector("a.level1"));

    for (WebElement element: subcategories) {
        if (element.getText().equals(subcategoryName)){
            element.click();
            break;
        }
    }
    return new Subcategory(driver);
}

But it won't go inside loop, probably because element.getText() is empty.

11
  • Maybe you are missing some delay / wait here? Commented Feb 15, 2022 at 11:43
  • Try with api.jquery.com/text or api.jquery.com/html methods Commented Feb 15, 2022 at 11:44
  • I tried to add wait but implicit wait get crossed out. Maybe I put wait on a wrong place. Where shoud it be? Commented Feb 15, 2022 at 11:49
  • @StefanItic I meant to put a delay, not implicit wait. Put it before the line where you collecting the elements. I.e. before List<WebElement> subcategories = driver.findElements(By.cssSelector("a.level1")); Commented Feb 15, 2022 at 12:01
  • @Prophet Sorry but i'm newbie here, so I don't know how to add delay. I just know about implicit and explicit waits... Can you give me the code that I should implement inside, plese? Commented Feb 15, 2022 at 12:05

2 Answers 2

1

To click on the WebElement with text as Shirts first you have to Mouse Hover the element with text as Men inducing WebDriverWait for the visibilityOfElementLocated and you can use the following locator strategies:

public Subcategory openSubcategory (String subcategoryName){

    WebElement menuMen = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@class,'has-children') and text()='Men']")));
    new Actions(driver).moveToElement(menuMen).build().perform();
    List<WebElement> subcategories = driver.findElements(By.xpath("//a[contains(@class,'has-children') and text()='Men']//following::ul[1]//li/a"));
    for (WebElement element: subcategories) {
        if (element.getText().equals(subcategoryName)){
            element.click();
            break;
        }
    }
    return new Subcategory(driver);
}
Sign up to request clarification or add additional context in comments.

2 Comments

OMG it's finally working!!!! Thank you sooo much!! Can you please explain to me how do you use xpath? I used it on a very simple way, but I don't understand everything you typed in xpath.
@StefanItic The element with text Men has the class level0 has-children where as I considered only has-children as I suspected level0 to be dynamic. Further the desired <ul> element in just the following element which have a descendant li -> a
1

element.getText() can be empty.

Try:

element.getAttribute("value");
// or
element.getAttribute("innerHTML");

Change your CSS to be:

"a[class*='level']";
// or
"a[]";

Debug the code and check if the element is not null

13 Comments

I tried both, not working
element.getAttribute("href")?
It might get href but i want text so I can call method and compare text inside link and text I typed. I tried btw and it didn't click on link i want to.
Try to get only element with the tag A. Debug the code and tell me what is written inside element.getAttribute("href")
I don't know how to find what is written inside...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.