Home > OS >  Python/Selenium print element value returns <session="xxx", element="xxx">
Python/Selenium print element value returns <session="xxx", element="xxx">

Time:03-06

I am trying to code a code that returns the weather of a city. For this I use selenium (I know there is better libraries but this is the one I am most comfy with). First I make the code search "weather xxx" and then use the google feature that automatically displays all infos.

Then I for example select the temperarture, there is th HTML, 3 is the value I want to print:

<span  id="wob_tm" style="display:inline">3</span>

But when I print it returns:

<selenium.webdriver.remote.webelement.WebElement (session="fb232f60015b08ee6db42b7fa83bd990", element="50c95ed2-17ce-41eb-835a-e7f9d0360540")>

How can I transform or navigate the output or xpath to get the value? (3)

Full code:

from selenium.webdriver.common.keys import Keys as k
from selenium import webdriver
from selenium.webdriver.common.by import By

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

def process():
    global temperature
    driver.find_element(By.XPATH,'//*[@id="L2AGLb"]/div').click()
    driver.find_element(By.NAME,'q').send_keys(city, ' weather' k.RETURN)
    temperature = driver.find_element(By.ID,'wob_tm')

city = input('City: ')
process()

print(temperature)
driver.quit()

P.S, I am using ID to locate the element but it returns the same "weird" output with XPATH etc...

Thanks for your help

CodePudding user response:

Your code is almost good. All you missing is extracting the text value from the web element.
This should work better:

from selenium.webdriver.common.keys import Keys as k
from selenium import webdriver
from selenium.webdriver.common.by import By

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

def process():
    global temperature
    driver.find_element(By.XPATH,'//*[@id="L2AGLb"]/div').click()
    driver.find_element(By.NAME,'q').send_keys(city, ' weather' k.RETURN)
    temperature = driver.find_element(By.ID,'wob_tm')

city = input('City: ')
process()

print(temperature.text)
driver.quit()

You will need to add waits here, preferably Expected Conditions explicit waits, as following:

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

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://google.com/")
wait = WebDriverWait(driver, 20)


def process():
    global temperature
    wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="L2AGLb"]/div'))).click()

    wait.until(EC.visibility_of_element_located((By.NAME, 'q'))).send_keys(city, ' weather' k.RETURN)
    temperature = wait.until(EC.visibility_of_element_located((By.ID, 'wob_tm'))).text

city = input('City: ')
process()

print(temperature)
driver.quit()

CodePudding user response:

thanks for all the answers, here is my finished code I completed with the help of @Prophet:

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

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://google.com/")
wait = WebDriverWait(driver, 20)

def process():
    global temperature
    global precipitation
    global wind
    global humidity
    global time
    global forecast

    #accepting google cookies and selecting language
    wait.until(EC.visibility_of_element_located((By.CLASS_NAME,'V5OCtd'))).click()
    if language == 'en':
        wait.until(EC.visibility_of_element_located((By.XPATH,'//*[@id="tbTubd"]/div/li[13]'))).click()
    if language == 'fr':
        wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="tbTubd"]/div/li[18]'))).click()
    if language == 'de':
        wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="tbTubd"]/div/li[9]'))).click()

    wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="L2AGLb"]/div'))).click()

    #searching for weather
    wait.until(EC.visibility_of_element_located((By.NAME, 'q'))).send_keys(city, ' weather' k.RETURN)

    #scraping weather data and assigning to variables
    temperature = wait.until(EC.visibility_of_element_located((By.ID, 'wob_tm'))).text
    wind = wait.until(EC.visibility_of_element_located((By.ID, 'wob_ws'))).text
    precipitation = wait.until(EC.visibility_of_element_located((By.ID, 'wob_pp'))).text
    humidity = wait.until(EC.visibility_of_element_located((By.ID, 'wob_hm'))).text
    time = wait.until(EC.visibility_of_element_located((By.ID, 'wob_dts'))).text
    forecast = wait.until(EC.visibility_of_element_located((By.ID, 'wob_dc'))).text


def info():

    #printing weather info
    print('Date: ', time)
    print('Forecast: ', forecast)
    print('Temperature: ',temperature,'°C')
    print('Wind: ',wind)
    print('Precipitation: ', precipitation)
    print('Humidity: ', humidity)

#city and language input
city = input('City: ')
language = input('Language [en,fr,de]: ')
if language != 'fr':
    if language != 'de':
         if language !='en':
            print('Language not supported')
            exit()
process()
info()

#leave driver
driver.quit()
  • Related