1

我是 Watir 的新手并且正在学习它。为了学习,我开发了一个脚本来进行注册,但要完成注册,我需要通过我的 GMAIL 帐户上发送的邮件进行验证。所以我已经安装了 ruby​​ GMAIL gem 并浏览了 github 的教程并在下面制作了示例脚本:

require "watir-webdriver"
require "gmail"
br = Watir::Browser.new :chrome
br.goto "gmail.com"
gmail = Gmail.new("sample@registration.com", "111111")
gmail.inbox.count(:unread)
gmail.inbox.click(:unread, :from => "noreply@registration.com").label("Confirm Verification")

但是脚本不起作用,所以可能是什么错误,以及我如何访问任何邮件并单击验证链接。

4

1 回答 1

2

您需要调整脚本以查找并阅读验证电子邮件。以下可用于阅读电子邮件。根据电子邮件的复杂程度和收件箱的内容,您可能需要进行一些调整。

verification_link = ''
Gmail.new("sample@registration.com", "111111") do |gmail|
    #Get the email the first email that is:
    #  unread and 
    #  from 'noreply@registration.com'
    email = gmail.inbox.emails(:unread, :from => 'noreply@registration.com').first

    #Get the message body, which will be in html
    message = email.body.decoded

    #Parse the message body for the link
    #  You may need to adjust the regex depending on the complexity of the email
    #  If the email is very complex, use Nokogiri to parse the html 
    verification_link = /<a.*?href="(.+?)".*?>Verify Now<\/a>/m.match(message)[1].gsub(/\s/, '')
    #=> "http://innovify.in/kwexcui/index.php?r=site/registerFirstSlap/key/3f21f205d603ce83e4dbd4667ff66a1f:2a/id/MTI2ODg2NjM4NTUyNw/lng/eng/email/maulik.goswami@bypeopletechnologies.in/companyName/R09PR0xFIFVLIExJTUlURUQ=/phoneNumber/012345678901234"
end

您将无法“点击”该链接。但是,它应该与在浏览器中导航到它相同 - 即让 watir 转到从电子邮件中提取的链接。

br = Watir::Browser.new :chrome
br.goto verification_link
于 2013-07-11T03:23:55.300 回答