【问题标题】:Automatic click on button with Selenium. Error raise TimeoutException使用 Selenium 自动单击按钮。错误引发 TimeoutException
【发布时间】:2022-01-01 10:58:08
【问题描述】:

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

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

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

PS:代码被设置为在“显示更多”按钮上单击多次,因为如果您第一次单击“显示更多”,页面会进一步向下滚动,但随后我又得到第二个“显示更多” “ 按钮。有时甚至是第三个“显示更多”。所以我还想在加载时点击第二个和第三个“显示更多”。

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

将 Firefox 与 Proxy 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("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)

【问题讨论】:

  • 这意味着它会消失并且没有更多的显示更多按钮。
  • @ArundeepChohan 你是什么意思?该按钮的字面意思是“Mostra più incontri”。你能帮我解决吗?谢谢
  • 如果您多次单击它,它会停止显示并且不再出现。然后你的代码超时。只需将其包装在尝试中,然后再中断。
  • @ArundeepChohan 我确保您单击了几次,因为每次单击“显示更多”时,页面滚动得更低,但随后会出现另一个“显示更多”。由于我是 Python 新手,您能告诉我答案中的代码吗?我当然会投票给你。谢谢

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

尝试如下。

使用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)

【讨论】:

  • 它不起作用
  • 抱歉,您的代码出错了。我得到 str '对象没有属性' sleep '。我该如何解决?谢谢 P.S:我删除了答案中的绿色勾号只是为了问你,然后我把它放回去了。谢谢
  • @heovan1999 - 如果是因为time.sleep(2),那么你需要import time
  • 我已经使用了导入时间,但我仍然遇到这个问题
  • 出于隐私和安全原因,您能否从您的答案中删除链接?也许用“链接”这个词或你想要的任何东西来代替它。抱歉请求。我提前谢谢你。 P.S:我只是为了写信给你,把答案中的绿色勾去掉,等你给我确认链接已被删除后,我会再把它放回去,这样我们就可以继续写了。谢谢
【解决方案2】:

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

  1. Scroll 所需高度。

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

  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
    

【讨论】:

  • 非常感谢你。好奇:代码被设置为在“显示更多”按钮上单击几次,因为如果您第一次单击“显示更多”,则页面会进一步向下滚动,但随后我会收到另一个“显示更多”按钮.有时甚至是第三个“显示更多”。您编写的代码仅在“显示更多”上单击一次?我还想在加载时单击第二个和第三个“显示更多”。谢谢
  • 代码是点击 Show more 按钮直到它出现。但是,代码逻辑必然会根据构建 DOM 的基础条件而有所不同。一项改进,而不是scrollTo(),尝试将scrollInToView() 用于某些可见元素,以便Show more 按钮出现在视口中。
  • 我尝试了您的代码,但仍然收到相同的错误消息。你还有其他建议吗?谢谢
  • 查看上面的评论,你必须尝试一些变化。
  • 我还没有看到你的评论,抱歉。我试过scrollInToView(),但我仍然得到同样的错误:(
猜你喜欢
  • 1970-01-01
  • 2016-04-29
  • 2023-04-03
  • 1970-01-01
  • 2023-04-11
  • 2023-01-24
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
相关资源
最近更新 更多