scoop realm
scoop realm

Reputation: 37

How to select element by specific child/descendant element

<a class="author " name="baut001" href="#!">
   <span class="content">
      <span class=" given-name">first name</span>
      <span class=" surname">Last name</span>
      <svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
         <path d="m22.6 77"></path>
      </svg>
   </span>
</a>
<a class="author" name="baut002" href="#!">
<span class="content">
<span class="text given-name">first name</span>
<span class="text surname">Last name</span>
<span class="author-ref" id="baff002"><sup>a</sup></span>
</span>
</a>
<a class="author" name="baut003" href="#!">
   <span class="content">
      <span class=" given-name">first name</span>
      <span class=" surname">Last name</span>
      <svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
         <path d="m22.6 77"></path>
      </svg>
   </span>
</a>

There are three <a> tags two among them contains <svg> tags, I want to click those elements.

Upvotes: 2

Views: 134

Answers (2)

Andersson
Andersson

Reputation: 52665

If you want to locate node that contains another node you might need to use XPath in below format

  1. By child node:

    //parent_node[child_node]
    

    or

    //parent_node[child::child_node]
    
  2. By descendant node:

    //anscestor_node[.//descendant_node]
    

    or

    //anscestor_node[descendant::descendant_node]
    

In your case to select a node that contains svg descendant node, you can try;

links_with_svg = driver.find_elements_by_xpath('//a[.//*[local-name()="svg"]]')

or

links_with_svg = driver.find_elements_by_xpath('//a[descendant::*[local-name()="svg"]]')

Upvotes: 1

Guy
Guy

Reputation: 50809

You can specify the <svg> tag in the locator.

You can use css_selector

driver.find_elements_by_css_selector('.author svg')

Or xpath

driver.find_elements_by_xpath('//a[@class="author"]//*[local-name()="svg"]')

Upvotes: 1

Related Questions