2013-03-08 27 views
5

外部URL我開始Seleniumlibrary測試玩(與robotframework運行),併爲我們的網站有廣告和指標等,我每次運行測試,這些網址得到加載。如何禁止裝載上seleniumlibrary/robotframework

有沒有辦法告訴硒/ robotframework不加載的URL的特定類型或防止它加載外部資源(即不是從本地主機的一切)。

回答

8

你可以用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() 
+0

你沒有說明你正在使用哪種語言,所以我認爲Java的。這是不正確的?快速搜索顯示該庫有很多用於python的包裝器。 – Scott 2013-03-12 13:24:10

+0

對不起,是的,我正在使用python。感謝您的迴應,一旦我嘗試了它,標記就會有效! – gforcada 2013-03-13 14:32:37

+1

你不應該逃避fbcdn.net點,例如: 'server.blacklistRequests( 「HTTPS://.* \\ \\ .fbcdn .NET /.*」,200);' – Lordalcol 2015-12-14 11:54:01