1

We've got some poorly written radio buttons on a legacy page as follows:

<input id="button1" name="button" value="32" onchange="assignProduct(this.value);" type="radio"></input>
RADIO TEXT 1
<input id="button2" name="button" value="33" onchange="assignProduct(this.value);" type="radio"></input>
RADIO TEXT 2

for information, this displays the same as would:

<input id="button" name="button" value="32" onchange="assignProduct(this.value);" type="radio">RADIO TEXT 1</input>
<input id="button" name="button" value="33" onchange="assignProduct(this.value);" type="radio">RADIO TEXT 2</input>

The latter code is more correct and allows selection of input button by text "RADIO TEXT 2" by:

driver.findElement(By.xpath("(//input[contains(@text, 'RADIO TEXT 2')])")).click();

What code can I use to find the radio button in the first code sample, which is what I actually have to deal with?

The text is no longer contained within the element so .xpath() doesn't match it.

I need to find the text, then click the input immediately prior? Can I do this without consuming and traversing the entire page?

3 Answers 3

1

Use XPath axes. In this particular case following-sibling. I've designed a locator to demonstrate it on this page. Look at the @bansalshah's answer. Lets select <p> where the following text contains for eg. It works, Ive checked this :)

.//*[@id='answer-25883774']//div[@class='post-text']/p[./following-sibling::*[contains(., 'for eg')]]

So, in your case, to select the input for RADIO TEXT 2 this should be something like

.//input[@type='radio'][./following-sibling::*[contains(., 'RADIO TEXT 2')]]

Sign up to request clarification or add additional context in comments.

Comments

1

For the first example, you can access buttons by there ID:

driver.findElement(By.id("button1")).click(); //first button

and

driver.findElement(By.id("button2")).click(); //second button

If the Id is not enough, you can, using xpath, access to them via id and value:

driver.findElement(By.xpath("//input[@id='button1'][@value='32']")).click(); //first button

and

driver.findElement(By.xpath("//input[@id='button2'][@value='33']")).click(); //first button

2 Comments

@CharlieS asks if he's able to make an XPath locator according to following text, not id or something else
as @AlexanderPetrovich comments, I don't necessarily know the id/value. In many html generating engines these might change with each build, but the text the end user will see is unlikely to change as often.
0

From the code which i can see you can map the values with text

for eg value=32 for radio text 1 and get the value from the xpath

if value =32 then select the radio button

Hope this helps

1 Comment

this does not answer the question. i don't know the value i want to find, i only know the text following. i could also select by id if i knew it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.