1

I want to get only " text ... " not using .split() or index slicing

HTML:

<a class="call_recipe" href="/recipes/2913">
      " text ... "
      <strong> something~ </strong>
    </a>

HTML Snapshot:

![

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Hank Park
  • 62
  • 3

3 Answers3

1

To print text ... you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR, childNodes and strip():

    print(driver.execute_script('return arguments[0].firstChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.call_recipe[href^='/recipes']")))).strip())
    
  • Using XPATH, get_attribute() and splitlines():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='call_recipe' and starts-with(@href, '/recipes')]"))).get_attribute("innerHTML").splitlines()[1])
    
  • 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
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0
driver.find_element_by_class_name("call_recipe").text

I think this is what you're after.

How to get text with selenium web driver in python

David Jay Brady
  • 1,034
  • 8
  • 20
0

you can utilize the

 "find_element_by_class_name("some_text").getText()"

or to better match the text you can use

"driver.find_element_by_xpath("..").text"

Hope this is helpful

Richardson
  • 74
  • 9