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



我正在使用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)

有什么想法吗?

我会这样写代码:

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

异常是由于您尝试导航到的 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>

最新更新