Skip to content

Instantly share code, notes, and snippets.

@velenux
Created May 17, 2011 16:51
Show Gist options
  • Save velenux/976835 to your computer and use it in GitHub Desktop.
Save velenux/976835 to your computer and use it in GitHub Desktop.
Select a link via XPath by it's content and container class
# encoding: utf-8
# using XPath 1.0, ref: http://www.exampledepot.com/egs/org.w3c.dom/xpath_GetElemByText.html
class XPathFactory
# build a XPath to find any link with content "content" inside a
# container with class "class_name"
def by_container_class_and_link_content(class_name, content)
"//*[@class and contains(concat(' ',normalize-space(@class),' ')," +
"' #{class_name} ')]//a[contains(translate(.,'abcdefghijklmnopqrstuvwxyz'," +
"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),'#{content.upcase}')]"
end
end
xpath_build = XPathFactory.new
element_xpath = xpath_build.by_container_class_and_link_content('find-me-class', 'Click me link')
puts element_xpath
# use in watir, etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment