selenium python运行firefox与TOR(windows)

时间:2015-05-15 16:18:31

标签: python selenium tor

我正试图用tor运行python selenium(firefox驱动程序)。 我检查了这个链接(Using Selenium WebDriver with Tor),但这是java实现。 我对selenium比较新,我在将java代码翻译成Python API时遇到了问题。

之前有人遇到过这个问题吗?

1 个答案:

答案 0 :(得分:1)

以下是适用于Python 2.7的代码(我最后一次更新于2015年3月12日)。作为额外的奖励,它填写agora上的用户名和密码字段,并要求您填写您看到的验证码,然后单击提交按钮。您需要创建一个默认firefox profile,将其代理设置为tor(127.0.0.1:9050),并将该配置文件名称的路径填入startbrowser函数分析器变量中:

from selenium import webdriver 
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.common.exceptions import NoSuchFrameException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

username = 'agora username'
password = 'agora password'
def go_to_page(br):
    #page_num = page_num - 1
    #start_results = page_num * 100
    #start_results = str(start_results)
    url = 'http://agorahooawayyfoe.onion'
    print '[*] loading url: '+url
    br.get(url)
    #br.get_screenshot_as_file('agora.tiff')
    usernamed = br.find_element_by_name("username")
    usernamed.send_keys(username) # password
    passwordd = br.find_element_by_name("password") # enterCaptcha
    passwordd.send_keys(password)
    captchad = br.find_element_by_name("enterCaptcha")
    capt = raw_input('enter captcha you see on screen: ')
    captchad.send_keys(capt)
    br.implicitly_wait(3)
    br.find_element_by_name("submit").click()

def start_browser():
    profiler = webdriver.FirefoxProfile('/Users/your username/Library/Application Support/Firefox/Profiles/vxh0qxtt.tor')
    br = webdriver.Firefox(firefox_profile=profiler)
    br.implicitly_wait(10)
    return br

def main():
    br = start_browser()
    go_to_page(br)

main()