'Selecting Different Web Elements

I am a Software Quality Assurance Engineer and I am trying to create an Automated test for a webpage.

Some background:

The framework of Selenium that my company uses ONLY allows you to use X paths saved as an object then you use pre-existing methods like "click (someobject)" or "enter (someobject)" etc.

Problem:

I'm currently trying to create a test that selects multiple buttons that are on the same class. There are 6 set buttons that I need to be able to select. Now I can do this but using:

`//*[@id="tenant-details-accordion"]/div[1]/div[2]/div/div[2]/div[1]/div/a
 //*[@id="tenant-details-accordion"]/div[1]/div[2]/div/div[2]/div[2]/div/a
 //*[@id="tenant-details-accordion"]/div[1]/div[2]/div/div[2]/div[3]/div/a
 //*[@id="tenant-details-accordion"]/div[1]/div[2]/div/div[2]/div[4]/div/a
 //*[@id="tenant-details-accordion"]/div[1]/div[2]/div/div[2]/div[5]/div/a
 //*[@id="tenant-details-accordion"]/div[1]/div[2]/div/div[2]/div[6]/div/a`

-However this is only temporary because the test will fail later down the road when a button is removed... I have talked to the Development team about adding Unique ID's to each button. But it does not seem like that is a path they want to go down...

Possible Solution:

  • Is it possible to narrow the ‘scope’ of Selenium?
    For example telling Selenium to look through a specific class instead of the entire page?
    -My thought is to have it search for a class, match a specific text, then select Set.

  • If yes, then also is it possible to combining multiple X path's Something like....

    //div[@class='col-sm-4'].... //div[contains(.,'Birth Date: Set +')]

My thought is that I could create an Xpath that narrows what Selenium will actually be looking through.

  1. Searching for the class

  2. Searching for Text "Birth Date"

  3. Selecting Set Button

Here are some pictures: FRONT END

Here is some HTML when I inspect the page HTML CODE



Solution 1:[1]

I believe you've already asked this and we've answered it.

But here are multiple solutions:

  1. //div[@class='col-sm-4' and contains(text(),'Birth Date')]/div/a

  2. //div[contains(@class,'col-sm-4') and contains(text(),'Birth Date']//a

there are many more options. Try and visit w3schools.com to learn more about xpath.

Solution 2:[2]

In order to first locate the div with class 'col-sm-4' and text as 'Birth Date:' and then find the link with text 'Set +' under it, any of the following XPATHs can be used:

//div[@class='col-sm-4' and contains(text(),'Birth Date')]/descendant::a[1]

Or

//div[@class='col-sm-4' and contains(text(),'Birth Date')]/descendant::a[contains(text(),'Set +')][1]

Or

//div[@class='col-sm-4' and contains(text(),'Birth Date')]/descendant::a[text()='Set +'][1]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 becixb
Solution 2 Mahipal