问题
I'm trying to retrieve the "onclick" attribute value from the below link:
<a onclick="test" href="myurl">aaa</a>
Using link.href
works fine (So I know link
is the correct object) however when using link.attribute_value("onclick")
what I get is a win32 object (puts shows #<WIN32OLE:0x2cbdf10>
instead of the "test"
string).
回答1:
If you don't get a neater solution, and it's a one off(If you can make sure the development process keeps this kind of testing in mind next time).Try:
onclick_value=browser.html[ %r{<a onclick="(.*?)" href="myurl">}mi, 1 ]
回答2:
This worked for me (Windows 2003, ruby 1.8.7, watir 1.6.5):
browser.link(:href => /myurl/).attribute_value("onclick")
# => "test"
回答3:
Tested on Mac with watir-webdriver gem driving Firefox:
browser.link(:href => "myurl").html.split('"')[3]
# => "myurl"
Tested on Windows with watir gem driving IE:
browser.link(:href => /myurl/).html.split('"')[1]
# => "myurl"
来源:https://stackoverflow.com/questions/3710550/how-do-i-retrieve-a-custom-attribute-in-watir