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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With