2014-07-17 136 views
0

我有一個div,其高度非常小。我無法訪問位於div底部的一些子元素。我試圖使用 element .elements.last.wd.location_once_scrolled_into_view滾動,但我無法滾動它。如何在運行時使用watirwebdriver更改div的Style屬性

然後我試着看看是否可以增加div的高度,以便腳本可以查看所有節點。原來的風格是style = 'overflow-y:auto;overflow-x:auto;width:150px;height: 350px;

的代碼我使用的修改是

element = @browser.div(:id => 'div:d') 
puts element.attribute_value('style') 
script = "return arguments[0].style = 'overflow-y:auto;overflow-x:auto;width:150px;height: 750px; '" 
@browser.execute_script(script, element) 
puts "edited height" 
puts element.attribute_value('style') 

我仍然看到風格值overflow-y:auto;overflow-x:auto;width:150px;height: 350px;。 運行時不能編輯div的高度嗎?

回答

2

問題

它看起來像arguments[0].style =是不是跨瀏覽器兼容。當我使用Firefox運行代碼時,高度已更新。相反,在Chrome中運行時,高度未更新。

這種差異被提及Mozilla的開發者頁面(https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.style)上:

除歌劇,樣式可以無法可以通過向 指定的字符串設定(只讀)風格的屬性,如elt.style =「color:blue;」。這是 ,因爲style屬性返回一個CSSStyleDeclaration對象。 相反,您可以設置樣式屬性是這樣的:,

script = "arguments[0].setAttribute('style', 'overflow-y:auto;overflow-x:auto;width:150px;height:750px;')" 

但是給出:

解決方案

要更改樣式屬性值,你最好使用setAttribute方法你只想改變身高,你可以簡單地做:

script = "arguments[0].style.height = '750px';" 
+0

'script =「arguments [0] .style.height ='750px';」 '工作得很好〜!非常感謝! – mkum