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

查看:26
本文介绍了如何让 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

但我收到以下错误:

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天全站免登陆