How do I retrieve a custom attribute in watir?

流过昼夜 提交于 2019-12-13 02:15:58

问题


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

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