2011-03-25 69 views
0

我可以在IE中使用watir-webdriver,但我更喜歡使用Firefox。 問題:我需要一個代理。 通過使用Google搜索,我發現了一些代碼片段,但我無法將它們放在一起。 這是我公司生產到現在,請讓我知道我在想什麼:Watir with webdriver,proxy,Firefox

require 'watir-webdriver' 

FirefoxProfile profile = new FirefoxProfile(); 
profile.setPreference("network.proxy.http", "proxy.myplace.com"); 
profile.setPreference("network.proxy.http_port", 8080); 
WebDriver driver = new FirefoxDriver(profile); 

browser = Watir::Browser.new :firefox 
browser.goto("http://www.google.com/") 

我收到此錯誤信息:

I:/watir/webdriver/webdrivertest.rb:3: syntax error, unexpected tCONSTANT, expec 
ting keyword_do or '{' or '(' 
FirefoxProfile profile = new FirefoxProfile(); 

另外,我不知道如何使用變量稱爲「驅動程序」

回答

4

調用底層的Selenium WebDriver。

我用這個技術來設置爲Firefox 3.6的一個路徑,以便我可以同火狐4.0和3.6測試:

Selenium::WebDriver::Firefox.path = ENV['FIREWATIRPATH'] 
browser = Watir::Browser.new :firefox 

所以,做你想做什麼:

profile = Selenium::WebDriver::Firefox::Profile.new 
proxy = Selenium::WebDriver::Proxy.new(:http => "http://proxy.org:8080") 
profile.proxy = proxy 

# You have to do a little more to use the specific profile 
driver = Selenium::WebDriver.for :firefox, :profile => profile 
browser = Watir::Browser.new(driver) 

看看:Selenium Ruby BindingsWebdriver FAQ欲瞭解更多信息。


您對Proxy線有什麼問題?

你可以嘗試this

profile = Selenium::WebDriver::Firefox::Profile.new 
profile["network.proxy.type"] = 1 
profile["network.proxy.http"] = "proxy.myplace.com" 
profile["network.proxy.http_port"] = 8080 

的想法是,看看你的設置是什麼:配置和代碼複製它們。

+0

看起來我們很接近。 這一行有個問題: proxy = Selenium :: WebDriver :: Proxy.new(:http =>「http://proxy.myplace.org:8080」) 錯誤消息是無效值Integer():「//proxy.unv.org:8080」 這意味着它將解釋第一個之後的任何內容:作爲端口號。也許我應該以另一種格式輸入http? – 2011-03-25 14:27:17

+0

另外,如果我跳過http://部分,並輸入代理爲「proxy.myplace.com:8080」,腳本將運行,但Firefox不會獲得代理設置,因此無法運行 – 2011-03-25 14:34:36

+0

我敢打賭,您可以執行此操作Proxy.new(:http =>'proxy.org',:http_port =>'8080') – 2011-03-25 14:34:56

-1

http://forum.iopus.com/viewtopic.php?t=12440#p36761

這說明我的iMacros使用的代碼,並且效果很好。我想你可以適應watir。

URL GOTO=about:config 
URL GOTO=javascript:var<SP>prefb<SP>=<SP>Components.classes["@mozilla.org/preferences-  service;1"].getService(Components.interfaces.nsIPrefBranch);var<SP>str<SP>= <SP>Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);str.data<SP>=<SP>"{{!COL2}}";prefb.setComplexValue("network.proxy.http",<SP>Components.interfaces.nsISupportsString,<SP>str);; 
2

在你原來的問題的基本問題是正確的錯誤消息

webdrivertest.rb:3: syntax error, unexpected tCONSTANT, expecting keyword_do or '{' or '(' 

Ruby解釋是看到在第三行腳本的,看起來像一個恆定的東西,在一個地方,它希望別的東西。

我懷疑這是ruby需要變量名的行的開始,並且你有一個類名。 Ruby期望以大寫字母開頭的變量是一個常量。這對於定義一個類很好,但不能創建一個實例,因爲實例不會是一個常量。

它看起來像你試圖做一個新的調用使用'新'關鍵字一些其他語言,而不是使用任何對象,你想做一個新的,紅寶石的方式。

比較代碼由Mike如他

profile = Selenium::WebDriver::Firefox::Profile.new 

經文的答案是你所要做的就行3

FirefoxProfile profile = new FirefoxProfile(); 

看看它們是如何不同?他是這樣做的。

+1

說得好。解決.new問題可能會導致一個稍微不同的路徑。 – 2011-03-28 13:25:28

1
profile = Selenium::WebDriver::Firefox::Profile.new 
profile.proxy = Selenium::WebDriver::Proxy.new :http => '12.12.12.12:8888', :ssl => '15.15.15.15:443' 
browser = Watir::Browser.new :firefox, :profile => profile