0

Do not swear strongly if such a question has already been, but I searched and came across this solution:

List<WebElement> list1 =  (List<WebElement>) d.findElements(by.classname("item-list-entry ng-scope")); 
list1.get(2).click();

I write to Java, and I try to parse the list, go through it and click on the 3rd element. Unfortunately, it does not help me.

The structure of my html

<div id="search-results" class="row search-result">
<div class="col-sm-12 col-wog">
<div class="item-list-entry ng-scope" ng-if="currentSearchOptions.display =='item-list'">
<div class="row ng-scope" ng-repeat="entry in searchResults">ITEM 1 </div>
<div class="row ng-scope" ng-repeat="entry in searchResults">ITEM 2 </div>
<div class="row ng-scope" ng-repeat="entry in searchResults">ITEM 3</div>
<div class="row ng-scope" ng-repeat="entry in searchResults">ITEM 4 </div>
</div>
</div>
</div>

I need the 3rd item. My decision before was simple (xpath)

//*[@id="search-results"]/div/div[1]/div[3]/div[2]/div[3]/p

 

2
  • What is your question? Why doesn't the xpath work anymore?
    – Jeni
    Commented Mar 23, 2019 at 20:49
  • How do I choose the 3rd container div? Xpath doesn't click on it anymore
    – Temp13
    Commented Mar 24, 2019 at 4:54

1 Answer 1

0

In the html you provided, I see no p so your xpath can't work, using the p. In the code you provide, you use By.classname. This kind of selector works only with a single class: the class of the element you try to select, you shouldn't chain classes like this "item-list-entry ng-scope". You can try the following Xpath:

 //*[@id="search-results"]//div[contains(@class,'item-list-entry')]/div[3]

Or you can use css selector:

#search-results .item-list-entry div:nth-child(3)

You seem to be using Java?

WebElement el =  d.findElement(By.cssSelector("#search-results .item-list-entry div:nth-child(3)")); 
el.click();
1
  • Today it worked. Thank. Leave this way)
    – Temp13
    Commented Mar 24, 2019 at 12:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.