【问题标题】:How to fix pac file not recognized for python selenium test?如何修复 python selenium 测试无法识别的 pac 文件?
【发布时间】:2019-02-02 02:11:12
【问题描述】:

由于我无法在这里简短地解释的原因,我想使用 pac 文件进行 firefox 测试 (python-selenium) 来协调使用的代理以处理各种类型的请求。

这是我正在使用的pac 文件:

function FindProxyForURL(url, host) 
{ 
    console.log("test proxy");
    lsjkd fhafh fasdfhsdjkl fhs
    if (url.substring(0, 3) === "ws:" || url.substring(0, 4) === "wss:") 
    { 
        return "DIRECT"; 
    } else { 
        return "PROXY 0.0.0.0:8080"; 
    }   
}

这是完整的测试脚本:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

selenium_proxy = webdriver.Proxy()
selenium_proxy.proxy_type = webdriver.common.proxy.ProxyType.PAC
selenium_proxy.proxyAutoconfigUrl = "/path/to/proxy.pac"

profile = webdriver.FirefoxProfile()
profile.set_proxy(selenium_proxy)

driver = webdriver.Firefox(firefox_profile=profile)
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
time.sleep(10)
driver.close()

因为pac 文件包含明显的语法错误,我预计测试会失败(如UnknownError)。但是,页面加载没有问题并且测试成功。

为什么 pac 文件不在 firefox 驱动程序中使用?还是已加载但被忽略?是否可以从pac 文件中记录一些文本?

我尝试像上面那样做,但是(即使没有语法错误)我在任何地方都看不到console.log(浏览器本身也看不到,为此我添加了 10 秒的睡眠,所以我可以按 F12 来检查浏览器的控制台)。

如何将pac 文件用于firefox?

附录:

您甚至可以使用以下pac 文件来查看它根本没有被使用:

//
// Define the network paths (direct, proxy and deny)
//

// Default connection
var direct = "DIRECT";

// Alternate Proxy Server
var proxy = "PROXY 0.0.0.0:8095";

// Default localhost for denied connections
var deny = "PROXY 0.0.0.0:65535";

//
// Proxy Logic
//

function FindProxyForURL(url, host) 
{           
    return deny;      
}

每个请求都应该被拒绝 - 但测试仍然有效并且网页被正确请求...

【问题讨论】:

    标签: python selenium firefox proxy pac


    【解决方案1】:

    解决办法是定义autoconfig URL!

    不得使用类似的路径

    selenium_proxy.proxyAutoconfigUrl = "/home/user/proxy.pac"
    

    不过是 URL 形式

    selenium_proxy.proxyAutoconfigUrl = "file:///home/user/proxy.pac"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 2020-10-12
      • 2017-05-13
      • 1970-01-01
      • 2012-01-19
      • 2021-03-30
      • 2013-10-12
      相关资源
      最近更新 更多