使用 Selenium 自动点击按钮。错误引发 TimeoutException

Automatic click on button with Selenium. Error raise TimeoutException

我有一个代码可以自动点击带有代理 TOR 的 Selenium e Firefox 页面底部的“显示更多”按钮,但我收到一个错误:

     raise TimeoutException (message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

代码好像写的不错,就是没看懂是什么问题。为了更清楚起见,我还共享了与我使用的代理的连接(一切正常,工作正常),然后是在出现错误时自动单击按钮的代码。你能帮我吗?谢谢

P.S:代码设置为多次点击“显示更多”按钮,因为如果你第一次点击“显示更多”,页面会向下滚动,但随后我再得到第二个“显示更多”按钮。有时甚至是第三个“显示更多”。所以我也想在加载时点击第二个和第三个“显示更多”

更新: cookie 屏幕是阴暗的,有阴影的,几乎是透明的黑色,所以也许这就是您的代码无法正常工作的原因。可能是 Tor 连接阻止了 cookie 的正常显示,你无法按下按钮(我想,也许,我不知道)

使用 Proxy Tor 连接 Firefox 的代码

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

可能 显示更多 按钮不再显示,因为所有记录都已显示。在这些情况下,理想的解决方案是:

  1. 要求的身高。

  2. 移动到网络元素。 (此步骤不是强制性的)

  3. 单击 显示更多 诱导 WebDriverWait

  4. 将代码封装在 try-except{} 块中

  5. 您的最佳代码块将是:

    WebDriverWait(driver, 12).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()        
    while True:
        try:
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            ActionChains(driver).move_to_element(WebDriverWait(driver, 12).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "ba.event__more.event__more--static")))).perform()
            WebDriverWait(driver, 12).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.event__more.event__more--static"))).click()
            print("Show more button clicked")
            continue
        except TimeoutException:
            print("No more Show more buttons")
            break
    

像下面这样尝试。

使用find_elementsShow more 元素存储在列表中。然后将列表的长度与 0 进行比较,以确定 Show more 按钮是否可以单击。

driver.get("URL")

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)