大家,我正在做一個廢料課程,基本上是我使用 webdriver 和 selenium 從網路中提取資料。直到昨天一切都按預期作業,今天我根本無法列印資訊,我嘗試尋找錯誤或一些建議,但我不知道一些提示,我無法在代碼中找到任何錯誤,正如我之前所說,直到昨天一切正常。
這是代碼:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
website = "https://www.adamchoi.co.uk/teamgoals/detailed"
path = r"H:/PROGRAMMING/Learning and Proyects/chromedriver_win32/chromedriver"
driver = webdriver.Chrome(executable_path= f"{path}")
driver.get(website)
all_matches_button = driver.find_element_by_xpath('//label[@analytics-event="All matches"]')
all_matches_button.click()
dropdown = Select(driver.find_element_by_id("country"))
dropdown.select_by_visible_text("Spain")
matches = driver.find_elements_by_tag_name("tr")
for match in matches:
match.txt
print(match.txt)
最后我收到這個:“H:\PROGRAMMING\anaconda\envs\Learning and Proyects\python.exe”“H:/PROGRAMMING/Learning and Proyects/Test scrape yelp.py”行程以退出代碼0結束
它應該列印匹配項 o,o
對不起,很長的帖子,我不知道如何解決它o,o
提前致謝 :)
uj5u.com熱心網友回復:
我看不出這段代碼是如何作業的。唯一可行的方法是逐步在除錯模式下運行它,而不是在正常運行模式下。
你在這里缺少等待。
這些最好是預期條件顯式等待。
請嘗試以下方法:
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
from selenium.webdriver.support.ui import Select
import time
website = "https://www.adamchoi.co.uk/teamgoals/detailed"
path = r"H:/PROGRAMMING/Learning and Proyects/chromedriver_win32/chromedriver"
driver = webdriver.Chrome(executable_path= f"{path}")
wait = WebDriverWait(driver, 20)
driver.get(website)
wait.until(EC.visibility_of_element_located((By.XPATH, '//label[@analytics-event="All matches"]'))).click()
wait.until(EC.visibility_of_element_located((By.ID, 'country')))
dropdown = Select(driver.find_element_by_id("country"))
dropdown.select_by_visible_text("Spain")
wait.until(EC.visibility_of_element_located((By.XPATH, '//tr')))
time.sleep(0.5)
matches = driver.find_elements_by_tag_name("tr")
for match in matches:
print(match.text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426934.html
