2011-03-25 70 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