2
如何在不使用js/jquery的情況下使用watir-webdriver更改href屬性值?Watir-webdriver:如何更改沒有js/jquery的屬性值
我可以得到一個屬性值:
@browser.frames[2].div(:id,"mid-2").link(:class,"btn-lrg").attribute_value("href")
但我也需要改變了一下href屬性值。
如何在不使用js/jquery的情況下使用watir-webdriver更改href屬性值?Watir-webdriver:如何更改沒有js/jquery的屬性值
我可以得到一個屬性值:
@browser.frames[2].div(:id,"mid-2").link(:class,"btn-lrg").attribute_value("href")
但我也需要改變了一下href屬性值。
我認爲修改鏈接的唯一方法就是使用javascript。由於該元素是使用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"
爲什麼你需要這樣做沒有js/jquery?我可以理解沒有jQuery,因爲它可能不可用。但是,JavaScript應該可用。 –
你會怎麼做手動? –
手動我去頁面的html代碼,並更改href值,然後單擊鏈接... – Annet