Stewart Moon
Stewart Moon

Reputation: 279

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

Upvotes: 2

Views: 134

Answers (3)

Mahipal
Mahipal

Reputation: 910

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]

Upvotes: 1

becixb
becixb

Reputation: 373

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.

Upvotes: 1

lauda
lauda

Reputation: 4173

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

Upvotes: 1

Related Questions