是否可以使用机器人打开浏览器,手动操作页面,然后继续使用机器人?
Is it possible to use a bot to open a browser, manually manipulate the page, then continue using a bot on it?
我正在使用 Ruby、Selenium WebDriver 和 Nokogiri 从网页中检索数据。加载正确的 HTML 后,我打印某个 class.
的内容
例如,
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(',')
我发现到目前为止最难的部分是正确加载正确的 HTML。例如,我想要的内容可能被某些 javascript 隐藏了,或者可能在不同的页面上。
是否可以使用 Selenium 加载页面,然后手动操作页面以显示正确的 HTML,然后然后 允许机器人完成并打印它应该打印的内容?
您可以使用 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
...
你可以很容易地做到这一点。我不熟悉ruby,但我会概述一下过程。
1) 启动驱动程序
2) 转到您的页面
3) 然后要求用户输入(在 python 2 中,例如:continue = raw_input('type something and hit enter here in the console to continue'))
4) 然后做你想做的所有其他事情。
当你执行这个脚本时,它会停在问题处。然后您可以手动操作浏览器,当您完成后,您可以转到 console/cmd window 并键入 "go" 并按回车键。然后它将从您手动离开浏览器的地方继续。
我正在使用 Ruby、Selenium WebDriver 和 Nokogiri 从网页中检索数据。加载正确的 HTML 后,我打印某个 class.
的内容例如,
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(',')
我发现到目前为止最难的部分是正确加载正确的 HTML。例如,我想要的内容可能被某些 javascript 隐藏了,或者可能在不同的页面上。
是否可以使用 Selenium 加载页面,然后手动操作页面以显示正确的 HTML,然后然后 允许机器人完成并打印它应该打印的内容?
您可以使用 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
...
你可以很容易地做到这一点。我不熟悉ruby,但我会概述一下过程。
1) 启动驱动程序 2) 转到您的页面 3) 然后要求用户输入(在 python 2 中,例如:continue = raw_input('type something and hit enter here in the console to continue'))
4) 然后做你想做的所有其他事情。
当你执行这个脚本时,它会停在问题处。然后您可以手动操作浏览器,当您完成后,您可以转到 console/cmd window 并键入 "go" 并按回车键。然后它将从您手动离开浏览器的地方继续。