如何在硒库机器人框架上禁止加载外部url?

问题描述 投票:5回答:1

我开始玩Seleniumlibrary测试(与robotframework一起运行),由于我们的网站有广告和指标等,每次我运行测试时,这些URL都会被加载。

有没有办法告诉seleniumrobotframework不要加载某些类型的URL,或者阻止它加载外部资源(即所有非本地host的资源)。

selenium-webdriver robotframework
1个回答
8
投票

你可以用 浏览器代理. 一旦你有安装, 你简单地设置代理和设置黑名单.

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);

我相信下面的工作将与 这个python包装器 (regex可能略有不同)。

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()
© www.soinside.com 2019 - 2024. All rights reserved.