0
votes

I'm trying to select a radio button based on the name of the address. Below I need to select either the span that has the name or the radio button itself. Either way looks like it will select the radio button. I need to do this based on the name within the span class. There are a random amount of spaces before the name of the person so instead of a StartsWith I've been trying to do this with a contains John Doe.

<div data-a-input-name="selectedAddress" class="a-radio">
   <label><input name="selectedAddress" value="113290190603" type="radio">
    <i class="a-icon a-icon-radio"></i>
     <span class="a-label a-radio-label">
         John Doe, Something RD, Some City, State, #####-####, Phone: #####
     </span>
   </label>
</div>

I can't seem to get the correct syntax regardless of all the ways I've tried. Here are some of the syntax I tried:

var Xpath = "//span[contains(@class,'a-label a-radio-label')][contains(text(),'" + shipName + "')]]";

            var radio = driver.FindElement(By.XPath(Xpath));
            radio.Click();

And this:

var Xpath = ".//input[following-sibling::strong[contains(.,'" + shipName + "')]]";
            var radio = driver.FindElement(By.XPath(Xpath));
            radio.Click();

I cannot point to a specific point with XPath because the item I am looking for might not necessarily be in the same ordered location. That is why I need to search for it by name. I also cannot do it by the value of the input as I do not know that information beforehand. I've actually tried many other variances of the two XPATH syntaxes shown above, but those are what I have left now as examples. Thanks for your help.

1
Your first XPath contains extra closing square bracket. To validate your second XPath provide with HTML code sample for input node (I don't see strong node in provided HTML) - Andersson

1 Answers

3
votes

You can try below XPath to locate required radio-button by partial text:

"//label[contains(.,'" + shipName + "')]"