Charles Belcher
Charles Belcher

Reputation: 121

How can I select the child of a sibling by XPath using Selenium?

Thanks in advance and apologies for my naivete:

I am attempting to select the text of an a element that is a child of a li, which is a sibling to a li that I have successfully targeted. My selector for the ancestor is:

var buyerName = detail.FindElement(By.XPath(".//*[(text()='Buyer:')]")).Text;

I have tried multiple iterations of the following, but I am currently trying:

var buyerEmail = detail.FindElement(By.XPath(".//*[(text()='Buyer:')following-sibling::li/a")).Text;

which tells me that the element does not exist. Essentially, the HTML structure that I am working with is such:

<ul>
   <li>Buyer:</li>
   <li>
       <a>link</a>
   </li>
</ul>

Would someone be so kind as to help explain what I have done wrong and how to correctly use XPath to arrive at/in the a element?

Upvotes: 2

Views: 2818

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193328

To extract the innerText i.e. link from the <a> tag, you need to to induce WebDriverWait for the ElementIsVisible() and you can use either of the following Locator Strategies:

  • Using XPath and Text property:

    var buyerName = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//ul/li[text()='Buyer:']//following::li[1]/a"))).Text;
    
  • Using XPath and GetAttribute() method:

    var buyerName = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//ul/li[contains(., 'Buyer:')]//following::li[1]/a"))).GetAttribute("innerHTML");
    

Upvotes: 0

JeffC
JeffC

Reputation: 25699

You were pretty close. I think the biggest issue was the extra parenthesis and the mismatched brackets.

There are a number of ways to locate this element... here's what I would use:

//li[.='Buyer:']/following-sibling::li/a

Here's how I build an XPath like this:

  1. I navigate to the page in Chrome and open the dev tools (F12). You can use other browsers but the commands are slightly different (or missing) and I think the Chrome dev tools are by far the best.

  2. Right-click on the desired element, in this case the A containing "link", and choose Inspect.

  3. From there, you can use several commands in the dev console... the ones I most commonly use are $x() to test XPaths and $$() to test CSS selectors.

  4. If the element that you want isn't unique on the page, start looking up the DOM until you find something unique. In this case it's the label, as you already pointed out, in the LI. Create the XPath to find just that element (below) and hit ENTER. That will show you all of the matching elements. You can expand that list and hover over each to see them highlighted on the page to ensure you are getting the right element.

    $x("//li[.='Buyer:']")
    
  5. From there we need to move to the sibling LI so we extend the XPath above to the below and hit ENTER to make sure we are still going down the right path.

    $x("//li[.='Buyer:']/following-sibling::li")
    
  6. From here we just need to move down one more level to the A

    $x("//li[.='Buyer:']/following-sibling::li/a")
    

Chrome dev tools console commands reference

Upvotes: 1

frianH
frianH

Reputation: 7563

Try following xpath:

//li[contains(text(), 'Buyer:')]//following-sibling::li/a

Upvotes: 0

Ed Bangga
Ed Bangga

Reputation: 13026

seems your xpath don't have closing brackets. Your almost near to the correct xpath.

//ul/li[text()='Buyer:']/following-sibling::li/a

Upvotes: 2

Related Questions