是否可以使用机器人打开浏览器,手动操作页面,然后继续使用机器人?

Joe*_*ano 8 ruby selenium screen-scraping webdriver nokogiri

我正在使用Ruby,Selenium WebDriver和Nokogiri从网页中检索数据.加载正确的HTML后,我打印某个类的内容.

例如,

require "selenium-webdriver"
require "nokogiri"
browser = Selenium::WebDriver.for :chrome
browser.get "https://jsfiddle.net"
doc = Nokogiri::HTML.parse(browser.page_source)
doc.css('.aiButton').map(&:text).join(',')
Run Code Online (Sandbox Code Playgroud)

到目前为止,我发现最困难的部分是正确加载正确的HTML.例如,我想要的内容可能被某些javascript隐藏,或者可能位于不同的页面上.

是否可以使用Selenium加载页面,然后手动操作页面以显示正确的HTML,然后允许机器人完成并打印它应该的内容?

jan*_*kan 2

您可以使用 Selenium 与网页交互 - 填写表单字段、单击按钮等。您甚至可以执行自己的 javascript 代码。

硒备忘单

编辑:

使用 pry 停止代码执行,以便您可以手动操作网页。

# Code for starting Selenium session and opening the web page
...

# Use pry to stop the code execution.
# Resume the program using command 'exit' in the pry context
require 'pry'; binding.pry

# Code to get results after you manually manipulate the web page
...
Run Code Online (Sandbox Code Playgroud)