我有一個代碼可以自動單擊帶有代理 TOR 的 Selenium e Firefox 頁面底部的“顯示更多”按鈕,但出現錯誤:
raise TimeoutException (message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

代碼好像寫的不錯,不明白是什么問題。為了更清晰,我還與我使用的代理共享連接(一切正常,作業正常),然后是代碼在出現錯誤的地方自動單擊按鈕。你能幫我嗎?謝謝
PS:代碼設定為在“顯示更多”按鈕上單擊多次,因為如果您第一次單擊“顯示更多”,則頁面會進一步向下滾動,但隨后我又得到了第二個“顯示更多”按鈕。有時甚至是第三個“顯示更多”。所以我還想在加載時單擊第二個和第三個“顯示更多”。
更新: cookie 螢屏是陰暗的,陰影的,幾乎是透明的黑色,所以也許這就是你的代碼不起作用的原因。也許 Tor 連接阻止了 cookie 的正常顯示,您無法按下按鈕(我想,也許,我不知道)

將 Firefox 與代理 Tor 連接的代碼
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
#Connect Firefox with Proxy Tor
torexe_linux = os.popen('/home/xxxx/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US')
profile = FirefoxProfile('/home/xxxx/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False) #certi la tengono True
profile.update_preferences()
firefox_options = webdriver.FirefoxOptions()
firefox_options.binary_location = '/usr/bin/firefox'
driver = webdriver.Firefox(
firefox_profile=profile, options=firefox_options,
executable_path='/usr/bin/geckodriver')
driver.get("https://www.diretta.it/calcio/svezia/allsvenskan/risultati/")
driver.maximize_window()
自動點擊代碼(問題出在這里)
from selenium.webdriver.common.action_chains import ActionChains
driver.implicitly_wait(12)
wait = WebDriverWait(driver, 12)
actions = ActionChains(driver)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
while(driver.find_elements_by_css_selector('a.event__more.event__more--static')):
show_more = driver.find_element_by_css_selector('a.event__more.event__more--static')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
actions.move_to_element(show_more).perform()
time.sleep(0.5)
show_more = driver.find_element_by_css_selector('a.event__more.event__more--static')
show_more.click()
time.sleep(3)
uj5u.com熱心網友回復:
嘗試如下。
使用find_elements的存盤Show more元素在串列中。然后將串列的長度與 0 進行比較,以確定Show more按鈕是否可以單擊。
driver.get("https://www.diretta.it/calcio/svezia/allsvenskan/risultati/")
wait = WebDriverWait(driver,30)
# Accept Cookies
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#onetrust-accept-btn-handler"))).click()
while len(driver.find_elements(By.CSS_SELECTOR,"a.event__more.event__more--static")) > 0:
showmore = driver.find_element(By.CSS_SELECTOR, "a.event__more.event__more--static")
driver.execute_script("arguments[0].scrollIntoView(true);", showmore)
showmore.click()
time.sleep(2)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/365968.html
標籤:Python 蟒蛇-3.x 硒 硒网络驱动程序 硒铬驱动器
