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

2023-12-04

我一直在尝试通过 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.nl and click()在所需的按钮上,您必须诱导WebDriver等待为了element_to_be_clickable()您可以使用以下任一方法定位策略:

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

tor_page

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python - 通过 Firefox 的 Tor 浏览器,无法单击按钮 的相关文章

随机推荐