简体   繁体   English

我可以使用打开的浏览器初始化Watir :: Browser对象吗?

[英]Can I initialize a Watir::Browser object with an open browser?

I am writing a small script with Watir to learn a bit about web scraping with scripting languages however the site I am using requires a login with 2 step verification. 我正在用Watir写一个小脚本,以学习一些有关使用脚本语言进行Web抓取的知识,但是我正在使用的网站需要登录并进行两步验证。

I have the login process automated however currently I am manually inserting the authenticator code then continuing with my script to obtain the information and complete the tasks required. 我的登录过程是自动化的,但是当前我手动插入身份验证码,然后继续执行我的脚本以获取信息并完成所需的任务。

Until I have automated the 2 step authentication it would be great if instead of opening a new browser and completing the login process I could just point a Watir::Browser object at an already logged in browser and start from there. 在我自动完成两步身份验证之前,如果不用打开新的浏览器并完成登录过程,我可以将Watir :: Browser对象指向一个已经登录的浏览器,然后从那里开始,那将是很好的选择。

For reference this is the code I am already using to create my browser object: 作为参考,这是我已经用于创建浏览器对象的代码:

require 'watir'

# Open ghost browser
browser = Watir::Browser.new(:chrome)

# Navigate to the scrape site
browser.goto("https://secure.site.that.requires.authentication.com")

# Set login information
browser.text_field(name: 'username').set "username"
browser.text_field(name: 'password').set "password"

# Login
browser.button(type: 'submit').click

# Manually enter verification code and continue script afterwards

No, Watir/Selenium can not interact wtih browsers that were not started by a driver in the current coding session. 不,Watir / Selenium无法与驱动程序在当前编码会话中未启动的浏览器进行交互。 You can do what you want by copying/pasting into the same irb session, or insert a sleep command to give you time to do the manual work in between the two different automated parts. 您可以通过复制/粘贴到同一个irb会话中来执行所需的操作,或者插入sleep命令以给您时间在两个不同的自动化部件之间进行手动工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM