0

This is the Python Selenium code I am trying to use to get the title of the articles:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://techwithtim.net")

search = driver.find_element_by_name("s")
search.send_keys("test")
search.send_keys(Keys.RETURN)

try:
    main = WebDriverWait(driver, 20).until(
             EC.presence_of_element_located((By.ID, "main"))
           )
    articles = main.find_elements_by_tag_name("article")
    for article in articles:
        header = article.find_elements_by_tag_name("a")[0]
        #print(header.get_attribute('href'))
        print(header.text)

finally:
    time.sleep(5)
    driver.quit()

The code is working well when extracting the href attribute, but it didn't work for the .text as I got empty lines instead of the headers of the articles

How can I fix that?

3

1 Answer 1

2

You may mean

print(header.get_attribute('innerHTML'))

To replace the ampersand sign, try

print(header.get_attribute('innerHTML').replace('&', '&'))

Or just use the innerText property:

print(header.get_attribute('innerText'))

Or the textContent Property:

print(header.get_attribute('textContent'))
Sign up to request clarification or add additional context in comments.

4 Comments

Amazing. Thanks a lot. How can I get the ampersand instead of GET & POST
I tried print(header.get_attribute('innerText')) and this works well. Thanks a lot for great help.
Thanks a lot. But what is the problem of using innerText as there may be other special characters.
Thank you very much for your awesome help. Best Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.