0

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...
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
inverted_index
  • 1,889
  • 16
  • 32

1 Answers1

0

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():

    print(driver.find_element_by_xpath("//h3[normalize-space()='Are you searching for specific keyword here?']//ancestor::a[1]").get_attribute("href"))
    
  • Using xpath and contains():

    print(driver.find_element_by_xpath("//h3[contains(., 'Are you searching for specific keyword here?')]//ancestor::a[1]").get_attribute("href"))      
    

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():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[normalize-space()='Are you searching for specific keyword here?']//ancestor::a[1]"))).get_attribute("href"))
    
  • Using xpath and contains():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[contains(., 'Are you searching for specific keyword here?')]//ancestor::a[1]"))).get_attribute("href"))
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217