Watir-webdriver: how to change attribute value without js/jquery

☆樱花仙子☆ 提交于 2019-12-10 16:46:39

问题


How can I change a href attribute value using watir-webdriver without using js/jquery?

I can get an attribute value:

@browser.frames[2].div(:id,"mid-2").link(:class,"btn-lrg").attribute_value("href")

But I also need to change a bit of the href attribute value.


回答1:


I think that the only way to modify the link is to use javascript. The code is quite maintainable since the element is retrieved using watir.

#Get the first link (or any element you want)
element = browser.frame.link

#Check element's initial attribute
puts element.attribute_value('href')
#=> "page_a.html"

#Execute javascript to change the attribute
script = "return arguments[0].href = 'page_b.html'"
browser.execute_script(script, element)

#Check that the attribute has changed
puts element.attribute_value('href')
#=> "page_b.html"


来源:https://stackoverflow.com/questions/17703979/watir-webdriver-how-to-change-attribute-value-without-js-jquery

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