我正在嘗試撰寫一個回傳城市天氣的代碼。為此,我使用 selenium(我知道有更好的庫,但這是我最滿意的一個)。首先,我進行代碼搜索“天氣 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 等相同的“奇怪”輸出......
謝謝你的幫助
uj5u.com熱心網友回復:
你的代碼幾乎是好的。您所缺少的只是從 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()
uj5u.com熱心網友回復:
感謝所有答案,這是我在@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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/438438.html
上一篇:如何使網頁停止加載并從中提取文本
下一篇:如何打開具有相同類的多個按鈕?
