【问题标题】:How to set proxy in Firefox Webdriver Permanently如何在 Firefox Webdriver 中永久设置代理
【发布时间】:2020-12-12 17:30:04
【问题描述】:

由于我无法直接从 Geckodriver 设置代理,我将使用手动更改它

from selenium import webdriver


myProxy = "xxxxxxxxx:yyyy"
ip, port = myProxy.split(":")


profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy_type',5)
profile.set_preference('network.proxy.http','xxxxxxxxx')
profile.set_preference('network.proxy.http_port',yyyy)
profile.update_preferences()

driver=webdriver.Firefox(firefox_profile=profile)
driver.get('http://www.google.co.th')
time.sleep(3)
driver.close()

我正在尝试执行,firefox已更改为代理模式

但是,当我通过机器人框架执行脚本时,它也通过 Geckodriver 执行,它不会变成代理。

我需要知道如何在 Geckodriver 中永久更改代理

【问题讨论】:

    标签: python selenium-webdriver robotframework


    【解决方案1】:

    使用 SeleniumLibrary 的 Open Browser 关键字,您可以像在 Python 脚本中那样设置任何首选项。

    例如,以下测试将使用这些选项每次打开浏览器:

    1. 代理 HTTP 端口设置为 777。
    2. 代理类型设置为“5”。
    3. 代理 HTTP“xxxxxxxxx”。

    *** Settings ***
    Library    SeleniumLibrary
    
    *** Test Cases ***
    Test
        Open Browser    http://example.com    Firefox   ff_profile_dir=set_preference("network.proxy_type", "5");set_preference("network.proxy.http", "xxxxxxxxx");set_preference("network.proxy.http_port", 777)   # Defining profile using FirefoxProfile mehtods.
        Sleep    1 min    reason=Verify proxy settings manually in the opened browser.
    

    睡眠期间在 Firefox 的 about:config 页面上检查的值:

    【讨论】:

    • 当我通过 Robot Framework 执行时,它无法执行
    • @Parit 它说什么?可能你的 SeleniumLibrary 版本太旧,请升级到 4.0.0 以上,最好升级到 4.5.0
    • 我已经升级了新版本的Robot Framework,以及RIDE Robot Framework。它捕获了文件“d:\lib\runpy.py”,第 193 行,在 run_module_as_main“main”,mod_spec)文件“d:\lib\runpy.py”中的错误,第 85 行,在 _run_code exec(code, run_globals) 文件“D:\Scripts\robot.exe__main_.py”中,第 5 行,在 ModuleNotFoundError: No module named 'robot' 我已经重新安装,重启笔记本电脑并设置环境变量。
    • @ParitKittirattanaviwat 升级 SeleniumLibrary 就足够了:pip install --upgrade robotframework-seleniumlibrarypip install robotframework-seleniumlibrary==4.5.0
    • @ParitKittirattanaviwat PYTHONPATH 和 PATH 环境变量是否设置正确?如果您重新安装后仍然找不到它,可能意味着它不知道在哪里找到它。
    猜你喜欢
    • 2017-01-13
    • 2015-07-29
    • 1970-01-01
    • 2011-11-25
    • 2017-02-15
    • 2013-01-20
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多