Python - Tor 浏览器通过 Firefox,无法点击按钮

Python - Tor Browser through Firefox, unable to click button

我一直在尝试通过 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

要在所需按钮上打开网页 http://dumpert.nlclick(),您必须为 element_to_be_clickable() 引入 WebDriverWait 并且您可以使用以下任一项 :

  • 使用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()
    
  • 浏览器快照:

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

对我来说,听起来你把它关掉了,因为许多按钮仍然适用于 JS。