Django_Tester
Django_Tester

Reputation: 139

Css selector or Xpath approach?

I'm attempting to write a css selector for an automated test using WebDriver for the following HTML block so I can select a radio button.

<span data-id="c1cf48c1-fbe3-44ea-ac5c-dfe588f0ed18" class="folder">
<input id="Folders" name="Folders" type="radio" value="c1cf48c1-fbe3-44ea-ac5c-dfe588f0ed18">               Contract Folder
</span>

I've attempted to write an xpath expression //*[contains(.,"Contract")] to no avail as there are too many elements being returned, and when I try //*[contains(.,"Contract Folder")] nothing is being returned. Therefore Im trying css which I would prefer to use. The value attribute is dynamic so Im unable to use that as a reference.

Can someone help advise what is the best approach in this scenario?

Many thanks.

Upvotes: 1

Views: 89

Answers (3)

paul trmbrth
paul trmbrth

Reputation: 20748

As element IDs are not unique in OP's use case (see comments for @alecxe's answer), you can use an XPath solution, based on the text content in the span:

.//span[@class="folder"][normalize-space(.)="Contract Folder"]/input[@type="radio"]

Upvotes: 2

GreyRoofPigeon
GreyRoofPigeon

Reputation: 18123

Maybe this is to easy thinking, but can't you give all your inputs the same class.

<input class="radioButton" ... />

input.radioButton { ... /* style */ ... }

Upvotes: 0

alecxe
alecxe

Reputation: 473863

Why don't get the input by id attribute?

//input[@id="Folders"]

Upvotes: 2

Related Questions