1

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)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Rein mV
  • 13
  • 3

1 Answers1

0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for answering my question. However, the code only prints the forecast after I've manually clicked 'accept' on the cookies. I've implemented the following code, so that selenium can do that for me. It however gives the error: `selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Accept"} (Session info: chrome=100.0.4896.75)` With the code: `acceptButton = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.LINK_TEXT, "Accept"))) acceptButton.click()` What could I improve? – Rein mV Apr 11 '22 at 09:05
  • Somehow I didn't face the cookie banner and it may be due to regional settings. Just change `presence_of_element_located()` to _`element_to_be_clickable()`_ and retest. – undetected Selenium Apr 11 '22 at 09:11
  • 1
    Haha damn those regional settings. Got it working with a vpn :) – Rein mV Apr 11 '22 at 10:20