Python/Selenium 打印元素值 returns <session="xxx", element="xxx"> 而不是所需的值
Python/Selenium print element value returns <session="xxx", element="xxx"> and not desired value
我正在尝试编写 returns 城市天气的代码。为此,我使用 selenium(我知道有更好的库,但这是我最喜欢的库)。首先,我让代码搜索“天气 xxx”,然后使用自动显示所有信息的 google 功能。
那我举个例子select温度,有thHTML,3就是我要打印的值:
<span class="wob_t q8U8x" id="wob_tm" style="display:inline">3</span>
但是当我打印它时 returns:
<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()
P.S,我正在使用 ID 来定位元素,但它 returns 与 XPATH 等相同的“奇怪”输出...
感谢您的帮助
你的代码差不多好了。您所缺少的只是从 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()
你需要在这里添加等待,最好是Expected Conditions显式等待,如下:
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()
感谢所有的回答,这是我在@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()
我正在尝试编写 returns 城市天气的代码。为此,我使用 selenium(我知道有更好的库,但这是我最喜欢的库)。首先,我让代码搜索“天气 xxx”,然后使用自动显示所有信息的 google 功能。
那我举个例子select温度,有thHTML,3就是我要打印的值:
<span class="wob_t q8U8x" id="wob_tm" style="display:inline">3</span>
但是当我打印它时 returns:
<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()
P.S,我正在使用 ID 来定位元素,但它 returns 与 XPATH 等相同的“奇怪”输出...
感谢您的帮助
你的代码差不多好了。您所缺少的只是从 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()
你需要在这里添加等待,最好是Expected Conditions显式等待,如下:
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()
感谢所有的回答,这是我在@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()