【发布时间】:2013-02-24 02:01:50
【问题描述】:
我开始使用 Seleniumlibrary 测试(使用 robotsframework 运行),并且由于我们的网站有广告和指标等,每次我运行测试时,这些 URL 都会被加载。
有没有办法告诉 selenium/robotframework 不加载某些类型的 URL 或阻止它加载外部资源(即不是来自本地主机的所有内容)。
【问题讨论】:
标签: selenium-webdriver robotframework
我开始使用 Seleniumlibrary 测试(使用 robotsframework 运行),并且由于我们的网站有广告和指标等,每次我运行测试时,这些 URL 都会被加载。
有没有办法告诉 selenium/robotframework 不加载某些类型的 URL 或阻止它加载外部资源(即不是来自本地主机的所有内容)。
【问题讨论】:
标签: selenium-webdriver robotframework
您可以使用browsermob-proxy 做到这一点。安装后,您只需设置代理并设置黑名单。
ProxyServer server = new ProxyServer(9000)
server.start();
final DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, server.seleniumProxy());
//Send a 200 for all requests to the facebook cdn
server.blacklistRequests("http://.*\\.fbcdn.net/.*", 200);
//Finish setting up your driver
WebDriver driver = new SomeDriverImpl(capabilities);
我相信以下内容适用于 this python wrapper(正则表达式可能略有不同):
from browsermobproxy import Server
server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()
proxy.blacklist('http://.*\\.fbcdn.net/.*', 200)
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
...
proxy.stop()
driver.quit()
【讨论】:
server.blacklistRequests("https?://.*\\.fbcdn\\.net/.*", 200);
现在可以在 Chrome 中用两行代码完成:
driver.execute_cdp_cmd('Network.setBlockedURLs', {"urls": ["www.baidu.com"]})
driver.execute_cdp_cmd('Network.enable', {})
【讨论】: