Having trouble using Capybara and Selenium to find an svg tag on a page

自古美人都是妖i 提交于 2019-12-30 07:02:07

问题


I had a test case like this:

  scenario "there should be an SVG tag" do
    ...
    page.find("svg")
  end

For some reason, Capybara could not find the svg tag even though when I looked in the page's source, the tag was there (and also visually).

I was only able to get it to find the SVG tag after I did something like:

  scenario "there should be an SVG tag" do
    ...
    page.find("#layers *[xmlns='http://www.w3.org/2000/svg']")
  end

(Note, the svg is within the "layers" ID).

Does anyone have any ideas? I use Selenium as the driver.


回答1:


It turns out this is an issue with Firefox's built in xpath evaluator.

Using FireBug, I was able to verify that the call that Selenium uses:

document.evaluate("//svg", document, null, 9, null).singleNodeValue

doesn't return any elements, whereas

document.evaluate("//div", document, null, 9, null).singleNodeValue

returns the first div on the page.

There may be some namespacing issues that could get FireFox to return svg elements. For now I've just looked for elements with my svg xmlns attribute.




回答2:


I have found a solution which enables the use of CSS selectors:


scenario "there should be an SVG tag" do
    ...
    Nokogiri::HTML.parse(page.body).css('svg')
end

Strange and annoying that it doesn't work out the box using page.find(), though.



来源:https://stackoverflow.com/questions/5433825/having-trouble-using-capybara-and-selenium-to-find-an-svg-tag-on-a-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!