Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 545
Annet Avatar asked Mar 23 '23 03:03

Annet


1 Answers

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"
like image 180
Justin Ko Avatar answered May 01 '23 06:05

Justin Ko