I'm trying to get an <a>
element, in case its nested (inner) <h3>
tag contains specific text. How should I do that? Suppose that the structure is like below:
<a href="https://example.com">
<section class="section">
<div class="section-content">
<div class="section-inner">
<h3>
Are you searching for specific keyword here?
</h3>
</div>
</div>
</section>
</a>
This HTML is a part of longer HTML text which is all inside a <body>
tag. Meaning that the source HTML has multiples of this structure, so one can iterate over these.
The search string is "you searching", so the element should be matched. After matching the element, I want to obtain a tag's href
's value which is: https://example.com
Currently, and after playing with this, I know how to match h3
tag by conditioning on the text inside it, but not sure how can I obtain href
's of parent <a>
tag after the matching is accomplished.
elem = driver.find_elements_by_xpath("//h3[contains(text(), 'you searching')]")
# elem is h3 tag...
To retrieve the value of
href
attribute of the<a>
tag with respect to the<h3>
tag text Are you searching for specific keyword here? you can use either of the following Locator Strategies:Using xpath and
normalize-space()
:Using xpath and
contains()
:Ideally you need to induce WebDriverWait for the
visibility_of_element()
and you can use either of the following Locator Strategies:Using xpath and
normalize-space()
:Using xpath and
contains()
:Note: You have to add the following imports :