使用 Nokogiri 打开从 XML 文件捕获的 URL

Open URL captured from XML file using Nokogiri

我正在使用 Nokogiri 和 Cucumber (selenium-webdriver),我有这个 XML 文件:

<root>
  <records>
    <record>
      <rowA>www.google.com</rowA>
    </record>
  </records>
</root>

然后,我创建这些步骤:

When /^I open my xml file$/ do
  file = File.open("example.xml")
  @xml = Nokogiri::XML(file)
  file.close
end

Then /^I should see the url google$/ do
  url = @xml.xpath('///rowA')  
  print(url[0].content)
  @browser.get url[0].content
end

使用这一行 @browser.get url[0].content 我试图使用从 XML 文件中捕获的 URL 打开浏览器,但我收到一个错误:

f.QueryInterface is not a function (Selenium::WebDriver::Error::UnknownError)

有什么想法吗?

异常是由于您尝试导航到 URL(而不是 URL 的检索方式)。

可以通过硬编码 URL 来重现异常以检索:

require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.get('www.google.com')
#=> f.QueryInterface is not a function (Selenium::WebDriver::Error::UnknownError)

您需要将 "http://" 添加到 URL:

require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.get('http://www.google.com')
#=> No exception

换句话说,XML 文档应更改为:

<root>
  <records>
    <record>
      <rowA>http://www.google.com</rowA>
    </record>
</root>

我会这样写代码:

When /^I open my xml file$/ do
  @xml = Nokogiri::XML(File.read("example.xml"))
end

Then /^I should see the url google$/ do
  @browser.get 'http://' + @xml.at('rowA').content
end