Ixi11
Ixi11

Reputation: 219

Selenium xpath assertion

I've got specific table view like:
enter image description here

I need to validate whether specific attributes: "TNT" / "Food" / "Another stuff" are visible & correctly displayed (string is fine). The DOM looks quite terrible:

    <div class="w-50" style="" xpath="1">
   <div style="">
      <!-- react-text: 231670 -->Categories<!-- /react-text --><!-- react-text: 231671 -->:<!-- /react-text -->
   </div>
   <div style="padding-left: 10px; display: flex;">
      <div style="">Juomat</div>
      <div style="margin-left: auto; margin-right: 100px;">126</div>
   </div>
   <div style="padding-left: 10px; display: flex;">
      <div>TNT</div>
      <div style="margin-left: auto; margin-right: 100px;">2</div>
   </div>
   <div style="padding-left: 10px; display: flex;">
      <div>Food</div>
      <div style="margin-left: auto; margin-right: 100px;">12</div>
   </div>
   <div style="padding-left: 10px; display: flex;">
      <div>Another Stuffs</div>
      <div style="margin-left: auto; margin-right: 100px;">2</div>
   </div>
</div>

I've tried to locate this: //div[@class = 'w-50'][2] (we got two the same classes as w-50) with adding word "contains" and somehow name those elements but in order to get Text from it i need to have it separately.

Can you tell me the xpath locator/s and how I can do the assertion in this case?

Upvotes: 0

Views: 224

Answers (2)

Sabari Cibi
Sabari Cibi

Reputation: 11

I am not much Familiar with C#, Also I somehow understand your issue.

Issue: To Check whether the List of Strings (products) available / visible in the webpage. Correct me if am wrong.

Below is the Java Code, convert the same to C#, if the logic is correct.

    List<String> list = new ArrayList<String>();
    list.add("Juomat");
    list.add("TNT");
    list.add("Food");
    list.add("Another Stuffs");
    // list.add -  add whatever products you needed later (dynamically if needed) 
    list.add("XXX"); 

    String xp = "//div[@class = 'w-50']";

    for(String item: list) {

        try {
                WebElement element = driver.findElement(By.xpath(xp + "/div/div[1][.='"+ item +"']"));
                if(item.equals(element.getText()))
                System.out.println(item +" is Visible");
                }

        catch(Exception e) {
                System.out.println(item + " is not Visible");               
        }           
    }

Upvotes: 1

Arild Andreassen
Arild Andreassen

Reputation: 329

It looks ugly, but

(.//*[@class='w-50']/div/div)[1]

should target the first element in the list, and you can assert that it has the text 'Juomat'

(.//*[@class='w-50']/div/div)[3]

will target the next element in the list, and you can assert that it has the text 'TNT'

repeat for the other elements in the list. This locator could also target the number if you wanted to

Upvotes: 1

Related Questions