seleniumxpathautomationqwebelement

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:

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

  • You would want something like this:

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

    Meaning select the link from the div that has a class with value col-sm-4 and contains the specified text.

    Or it could also work like this:

    //div[contains(text(), 'Birth Date')]/a