【问题标题】:Same webdriver, multiple instances, each with its own profile相同的 webdriver,多个实例,每个实例都有自己的配置文件
【发布时间】:2016-07-11 05:37:01
【问题描述】:

我需要知道是否可以为每个驱动程序句柄设置代理/配置文件。 我有以下代码:

    def browser(url, id, user1, parolauser1, user2, parolauser2):
    print multiprocessing.current_process()
    fp = webdriver.FirefoxProfile("C:\Users\yt\AppData\Roaming\Mozilla\Firefox\Profiles\hgv9qvsn.default")
    driver = webdriver.Firefox(fp)
    driver.get(url)
    for handle in driver.window_handles:
        if "whateveriwant" in driver.title and id == 0:
            ...
            for catevreaueu in range(0,7000):
                test_to_see_how_many_lines_are_in_a_file = function_test_to_see_how_many_lines_are_in_a_file_for_this_first_handle()
                print test_to_see_how_many_lines_are_in_a_file
                if test_to_see_how_many_lines_are_in_a_file < 3:
                    another_function_for_continuing_on_this_first_handle(driver)
                else:
                    driver.close()
                    break
        if "whateveriwant" in driver.title and id == 1:
            ...
            for catevreaueu in range(0,7000):
                test_to_see_how_many_lines_are_in_a_file = function_test_to_see_how_many_lines_are_in_a_file_for_this_s_handle()
                print test_to_see_how_many_lines_are_in_a_file
                if test_to_see_how_many_lines_are_in_a_file < 3:
                    another_function_for_continuing_on_this_second_handle(driver)
                else:
                    driver.close()
                    break
    return
if __name__ == '__main__':
    jobs = []
    user_one = "userone"
    parola_user_one = "passwordforuserone"
    user_two = "usertwo"
    parola_user_two = "passwordforuser2"
    pages = ["http://www.didi.com", "http://www.didi.com/"]
    id = 0
    for page in pages:
        p = multiprocessing.Process(target=browser, args=(page, id, user_one, parola_user_one, user_two, parola_user_two))
        id+=1
        jobs.append(p)
        p.start()

现在它启动了两个 webdriver 实例,但您可以看到它们都使用相同的配置文件。

【问题讨论】:

    标签: python python-2.7 selenium webdriver


    【解决方案1】:

    虽然它有时可能会起作用,但我不建议这样做,如果您需要相同的配置文件,请创建多个副本。如果您查看underlying code,您会发现仅创建一个 webdriver 实例会更改配置文件中的一些内容。 driver.quit 也完全删除个人资料。

    【讨论】:

    • 它有效,但我不知道如何为每个设置代理
    • 正如我所说,它有时可能会起作用,但它不是那么稳定。代理就是这些示例之一 - 因为 Firefox 在配置文件中设置了它,并且它们是共享的。
    猜你喜欢
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多