Python/Selenium 打印元素值返回 而不是期望值

发布于 2025-01-11 22:26:50 字数 1125 浏览 0 评论 0 原文

我正在尝试编写一个返回城市天气的代码。为此,我使用 selenium (我知道有更好的库,但这是我最满意的库)。首先,我使用代码搜索“weather xxx”,然后使用自动显示所有信息的谷歌功能。

然后我例如选择温度,有 th HTML,3 是我想要打印的值:

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

但是当我打印时它返回:

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

如何转换或导航输出或 xpath 来获取值? (3)

完整代码:

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()

PS,我使用 ID 来定位元素,但它返回与 XPATH 等相同的“奇怪”输出...

感谢您的帮助

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 class="wob_t q8U8x" 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

机场等船 2025-01-18 22:26:50

你的代码几乎是好的。您所缺少的只是从 Web 元素中提取文本值。
这应该会更好:

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()

您需要在此处添加等待,最好是预期条件显式等待,如下所示:

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()

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()
铜锣湾横着走 2025-01-18 22:26:50

感谢您的所有答案,这是我在@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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文