0

How to find article on website by h1 and p text like on image below?

I tried this, where I can found all articles and I don't know how to find this one with text in h1 and by text in p. And then I would like to click on this.

text = driver.find_elements_by_xpath("//article/div[contains(@class,'inner-article')]/h1")

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Filip
  • 2,191
  • 3
  • 12
  • 25

2 Answers2

2

To extract and print the text Beanie Custom First and Red you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and text attribute:

    • Printing Beanie Custom First:

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "article.inner-article h1 > a.name-link[href='/shop/asd']"))).text)
      
    • Printing Red:

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "article.inner-article p > a.name-link[href='/shop/asd']"))).text)
      
  • Using XPATH and get_attribute():

    • Printing Beanie Custom First:

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//article[@class='inner-article']//h1/a[@class='name-link' and @href='/shop/asd']"))).get_attribute("innerHTML"))
      
    • Printing Red:

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//article[@class='inner-article']//p/a[@class='name-link' and @href='/shop/asd']"))).get_attribute("innerHTML"))
      
  • 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
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


Outro

Link to useful documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1
text = driver.find_elements_by_xpath("//article/div[contains(@class,'inner-article')][h1/a[contains(text(),"Beanie")]][p/a[contains(text(),"Red")]]")

you can use above xpath, which will check whehter the parent element article/div has child elements h1/a and p/a with texts Beanie and Red respectively

in w3chool html editor is inside iframe so switch to iframe in your seelnium tests before tryng to find the element

PDHide
  • 18,113
  • 2
  • 31
  • 46