问题
I know you can search text in html using wildcards. Can you search for attribute values in html using wildcards with nokogiri
e.g., suppose I want to search for classes with value *session*
回答1:
You can use xpath contains() function to search the document. Something like:
doc.xpath("//*[@*[contains(., 'session')]]").each do |ele|
# something
end
This search returns all the elements with any attribute whose value contains the string 'session'.
回答2:
Had a similar problem few days ago - notice spaces around class values.
find(:xpath, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-edit ')]")
来源:https://stackoverflow.com/questions/11593489/can-you-search-html-attributes-using-wildcards-with-ruby-nokogiri