定义后,我能这样写代码
def submit_order
s('button#submit').click
end
The short method name "s" is inspired by jQuery. Here it will keep polling the DOM for 10 seconds until it finds the button with id "submit". It's like implicit wait but only for finding one element. When you really need to wait for multiple elements, you can use an explicit wait, which, to me, makes more sense than a hidden implicit one.
短方法 s 的灵感是来源于jQuery。 它会轮询DOM 10秒,直到它在找到一个id 为'submit'的button 为止。当只找一个元素时,用隐式等待是恰当的,当你真的需要定位多个对象的时候,你应该使用显示等待,对我来说,这样有意义。
译者注:关于显式等待和隐式等待的区别,请看下面讨论及文档,有助于理解我在翻译什么... : (
http://groups.google.com/group/selenium-users/browse_thread/thread/fd0cb59b953f5e94/123380cef05d7bdb?lnk=raot
http://seleniumhq.org/docs/04_webdriver_advanced.html
Set the initial browser window size when using Chromedriver.
当在使用Chromedirver时,设定浏览器窗口初始化大小
Ruby code:
profile = Selenium::WebDriver::Chrome::Profile.new
profile['browser.window_placement.top'] = 0
profile['browser.window_placement.left'] = 0
profile['browser.window_placement.right'] = 1024
profile['browser.window_placement.bottom'] = 768
driver = Selenium::WebDriver.for :chrome, profile: profile
This works in both Windows and OSX (will try Linux and update here)
Bad news for Java, C# and Python coders though, it seems that as of now setting chrome preference is not supported in the java version of Webdrive. Your best chance could be creating a ChromeProfile class based on the exiting FirefoxProfile class.
以上的代码在windows 和 OSX 上都工作正常。坏消息是,对于Java , C# 和Python 的coder 来说,看上去现在Java版本的webdriver还不支持设定chrome 的属性。 你好是基于现有的FirefoxProfile 类来创建一个ChromeProfile 类。
Scroll to a button before clicking it.
滚动到相应的button后才点击
Clicking buttons sometimes randomly fail. It could be caused by the fact that the button is out of the view area at the moment you command selenium to click it. One way to address it is to always scroll to it before clicking it.
点击button有时候会无故失败,这可能是由于selenium 执行点击命令的时候,这个button在视图之外。所以解决这个问题的其中一个方法是,滚动到相应的button后才点击。