【问题标题】:Python - Tor Browser through Firefox, unable to click buttonPython - 通过 Firefox 的 Tor 浏览器,无法点击按钮
【发布时间】:2020-04-17 13:24:51
【问题描述】:

我一直在尝试通过 Tor 浏览器作为代理通过 Firefox 访问某个站点 (dumpert.nl)。我使用 Tor 浏览器的原因是我每次进入网站时都可以使用不同的 IP 地址进入网站。我知道这是可能的,但我还没有找到这样做的方法。我找到了多种方法来做到这一点,但它们(尚未)对我有用。这部分也需要帮助。

真正的问题是我在使用本网站的接受 Cookie 页面时遇到问题。当我手动单击按钮接受 cookie 时,什么也没有发生。我无法进入下一页。如果我使用 Selenium 的 .click() 函数也没有任何反应,则页面已完全加载,所以这不是问题。这些按钮由于某种原因不起作用,我不知道为什么。不知道是 Tor 问题还是通过 Tor 使用 Firefox 的问题。

我使用以下代码导航到网站:

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    import os
    
    torexe = os.popen(r'C:\Users\nick\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
    profile = FirefoxProfile(r'C:\Users\nick\Desktop\Tor Browser\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)
    profile.update_preferences()
    driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\Webdrivers\geckodriver.exe')
    driver.get("http://dumpert.nl")
    driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/a").click() #cookie click

    #Rest of my code doing stuff not important for this issue

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:

    要在所需按钮上打开网页http://dumpert.nlclick(),您必须为element_to_be_clickable() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

    • 使用CSS_SELECTOR

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
      import os
      
      torexe = os.popen(r'C:\Users\Soma Bhattacharjee\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
      profile = FirefoxProfile(r'C:\Users\Soma Bhattacharjee\Desktop\Tor Browser\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)
      profile.update_preferences()
      driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\WebDrivers\geckodriver.exe')
      driver.get("http://dumpert.nl")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.approve-btn[title^='And yes']>span"))).click()
      
    • 使用XPATH

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
      import os
      
      torexe = os.popen(r'C:\Users\Soma Bhattacharjee\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
      profile = FirefoxProfile(r'C:\Users\Soma Bhattacharjee\Desktop\Tor Browser\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)
      profile.update_preferences()
      driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\WebDrivers\geckodriver.exe')
      driver.get("http://dumpert.nl")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='approve-btn']/span[starts-with(., 'Yes')]"))).click()
      
    • 浏览器快照:

    【讨论】:

    • 它正在运行代码并在没有任何错误的情况下结束代码。然而,代码实际上无法进入下一个屏幕。如果我尝试手动单击 cookie 接受按钮,也不会发生任何事情。就像我根本无法点击一样。此时页面已完全加载。这可能是我的 Tor 浏览器中的错误选项吗?
    • @Grasmat 如果 ... 手动单击 cookie 接受按钮也没有任何反应... 可能问题出在您的 TOR 浏览器/安装上。该程序在我结束时执行良好。
    • 必须是,重新安装 Tor 浏览器和 Firefox 并没有成功。于是搜索继续。无论如何,感谢您迄今为止的帮助
    【解决方案2】:

    我认为在隐私选项卡中有一个选项,您可以在其中关闭 javascript。

    对我来说,这听起来像是你关掉了它,因为许多按钮仍然可以使用 JS。

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2018-10-22
    • 2020-07-31
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多