简体   繁体   English

自动点击带有 Selenium 的按钮。错误引发 TimeoutException

[英]Automatic click on button with Selenium. Error raise TimeoutException

I have a code to automatically click on the "Show more" button at the bottom of a page with Selenium e Firefox with proxy TOR, but I get an error:我有一个代码可以自动点击带有代理 TOR 的页面底部的“显示更多”按钮 Selenium e Firefox,但我收到错误消息:

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

在此处输入图像描述

The code seems to be written well, I don't understand what the problem is.代码好像写的不错,就是不明白是什么问题。 For greater clarity, I also share the connection with the proxy I use (everything ok, works fine) and then the code to click on the button automatically where I have the error.为了更清楚起见,我还共享了与我使用的代理的连接(一切正常,工作正常),然后是在我出现错误的地方自动点击按钮的代码。 Can you help me please?你能帮我吗? Thanks谢谢

PS: The code was set to click several times on the "Show more" button, because if you click on "Show more" the first time, then the page scrolls further down, but then I get another second "Show more" button. PS:代码被设置为在“显示更多”按钮上点击几次,因为如果你第一次点击“显示更多”,页面会向下滚动,但随后我又得到了另一个“显示更多”按钮。 Sometimes even a third "Show more".有时甚至是第三个“显示更多”。 So I would also like to click on the second and third "Show more" when they are loaded.所以我也想在加载时点击第二个和第三个“显示更多”。

UPDATE: the cookie screen is shady, shaded, almost transparent black, so maybe that's why your code isn't working.更新: cookie 屏幕是阴暗的,有阴影的,几乎是透明的黑色,所以也许这就是您的代码无法正常工作的原因。 Maybe the Tor connection prevents the normal display of cookies and you can't press the button (I think, maybe, I don't know)也许 Tor 连接阻止了 cookies 的正常显示,您无法按下按钮(我想,也许,我不知道)

在此处输入图像描述

Code for connect Firefox with Proxy Tor使用 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()

Code for automatic click (THE PROBLEM IS HERE)自动点击代码(问题出在这里)

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)

Possibly the Show more button no more shows up as all the records are already being shown.可能不再显示显示更多按钮,因为所有记录都已显示。 In those cases, an ideal solution would be to:在这些情况下,理想的解决方案是:

  1. Scroll the required height. 滚动所需的高度。

  2. Move to the webelement.移动到 web 元素。 (this step isn't mandatory) (此步骤不是强制性的)

  3. Click on Show More inducing WebDriverWait点击Show More诱导WebDriverWait

  4. Wrap up the code in a try-except{} block将代码封装在try-except{}块中

  5. Your optimum code block will be:您的最佳代码块将是:

     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

Try like below.尝试如下。

Use find_elements to store the Show more element in a list.使用find_elementsShow more元素存储在列表中。 Then compare the length of the list to 0, to determine if the Show more button is available to click.然后将列表的长度与 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Selenium。 无法点击按钮 - Python Selenium. Can't click a button Python Selenium。 无法点击按钮 - Python Selenium. Can't click on a button selenium.common.exceptions.TimeoutException:当我尝试使用python进行按钮单击时,将出现此错误 - selenium.common.exceptions.TimeoutException: this error will getting when I'm trying to button click using python 尝试单击带有 selenium 的按钮时出现“无法找到元素”或“TimeoutException”错误 - "Unable to locate element" or "TimeoutException" errors when trying to click on a button with selenium Python Selenium Explict等待不会引发TimeoutException - Python Selenium Explict wait doen not raise TimeoutException Selenium,Python(引发TimeoutException(消息,屏幕,堆栈跟踪)TimeoutException) - Selenium, Python (raise TimeoutException(message, screen, stacktrace) TimeoutException) Selenium TimeoutException 未处理错误 - Selenium TimeoutException not handling error 如何使用硒单击图像链接导致TimeoutException错误 - How to click on an image link using selenium resulting in TimeoutException error 使用 selenium 导航网站。Click() 不工作 - Navigate website with selenium. Click() not working Instagram 和硒。 如何点击灯箱? - Instagram and Selenium. How to Click Lightbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM