如何让Selenium/Ruby机器人在执行操作之前等待? [英] How to get a Selenium/Ruby bot to wait before performing an action?

查看:89
本文介绍了如何让Selenium/Ruby机器人在执行操作之前等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个点击元素的Selenium/Ruby网络机器人.问题是,有时机器人没有足够的时间加载页面,然后机器人决定找不到该元素.

I'm building a Selenium/Ruby web bot that clicks on elements. The problem is, sometimes there isn't enough time for the page to load before the bot decides it can't find the element.

在执行操作之前让Selenium等待的Ruby方法是什么?我希望显式等待,但是隐式等待也可以.

What's the Ruby way to get Selenium to wait before performing an action? I would prefer explicit waiting, but I'm fine with implicit waiting too.

我尝试使用wait.until方法:

require "selenium-webdriver"
require "nokogiri"
driver = Selenium::WebDriver.for :chrome
wait = Selenium::WebDriver::Wait.new(:timeout => 15)
driver.navigate.to "http://google.com"
driver.wait.until.find_element(:class, "gb_P").click

但是我遇到了以下错误:

But I'm getting the following error:

Undefined method 'wait' for <Selenium::WebDriver>


我也尝试过:


I also tried:

require "watir-webdriver/wait"
...
driver.find_element(:class, "gb_P").wait_until.click

但这也给了我一个未定义的方法错误:

but that's also giving me an undefined method error:

undefined method `when_present' for #<Selenium::WebDriver...>

推荐答案

您正在将wait用作WebDriver函数,但事实并非如此.试试这个

You are using wait as WebDriver function, but it isn't. Try this

element = wait.until { driver.find_element(:class => "gb_P") }
element.click

这篇关于如何让Selenium/Ruby机器人在执行操作之前等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆