Home > Back-end >  Selenium finds element by class, but returns empty string. How to fix?
Selenium finds element by class, but returns empty string. How to fix?

Time:04-11

The code tries to show the weather forecast for a city. It is able to find the class with the content, but it prints out an empty string. Why is that and how could I change my code to not get an empty string?

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

s=Service("C:\Program Files (x86)\chromedriver.exe")
browser = webdriver.Chrome(service=s)

city = str(input("Enter a city"))

url="https://www.weather-forecast.com/locations/" city "/forecasts/latest"
browser.get(url)
browser.maximize_window()

content = browser.find_element(By.CLASS_NAME, "b-forecast__table-description-content")
print(content.text)

CodePudding user response:

You were close enough. The contents are actually within the descendant <span> of the ancestor <p> tags.


To print all the desired texts you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    city = "Dallas"
    driver.get("https://www.weather-forecast.com/locations/" city "/forecasts/latest")
    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "p.b-forecast__table-description-content > span.phrase")))])
    
  • Using XPATH:

    city = "Dallas"
    driver.get("https://www.weather-forecast.com/locations/" city "/forecasts/latest")
    print([my_elem.get_attribute("innerText") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//p[@class='b-forecast__table-description-content']/span[@class='phrase']")))])
    
  • Console Output:

    ['Heavy rain (total 0.8in), heaviest during Tue night. Warm (max 86°F on Mon afternoon, min 66°F on Tue night). Winds decreasing (fresh winds from the S on Sun night, light winds from the S by Mon night).', 'Light rain (total 0.3in), mostly falling on Fri morning. Warm (max 77°F on Wed afternoon, min 57°F on Wed night). Wind will be generally light.', 'Light rain (total 0.1in), mostly falling on Sat night. Warm (max 84°F on Sat afternoon, min 43°F on Sun night). Winds decreasing (fresh winds from the N on Sun afternoon, calm by Mon night).', 'Mostly dry. Warm (max 70°F on Wed afternoon, min 48°F on Tue night). Wind will be generally light.']
    
  • 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
    
  • Related