2016-07-31 73 views
3

使用:與PhantomJS硒在Python修改與execute_script在硒的樣式屬性,但屬性的值不會改變

我需要設置一個輸入標籤爲「」,因爲它被設置爲一個樣式屬性「display:None」,它阻止我用Selenium中的send_keys填充輸入。

我正在使用execute_script來實現這一點。 execute_script運行,但style屬性保持不變。爲什麼PhantomJS不改變樣式屬性?

HTML風格屬性我想刪除

<input type="password" size="10" id="navbar_password" name="vb_login_password" tabindex="102" class="textbox" style="display: none;"> 

的Python腳本硒:

爲什麼不style屬性的值由execute_script改變?

password = driver.find_element_by_name("vb_login_password") 

driver.execute_script("arguments[0]['style'] = arguments[1]", password, '') 

print(password.get_attribute("style")) 

//display:none; 
+0

** **更新:我想上的用戶名輸入字段改變各種屬性,它的工作每一次,但密碼輸入字段不能改變......我知道它與顯示有關:無屬性,但我不知道如何補救它,如果我不能修復它。 也許我應該嘗試從dom中刪除該元素,然後插入沒有該樣式屬性的副本? –

回答

1

嘗試如下: -

password = driver.find_element_by_name("vb_login_password") 

password = driver.execute_script("arguments[0].style.display = 'block'; return arguments[0];", password) 

print(password.value_of_css_property("display")) 

#now you can set value using send_keys 
password.send_keys("your value"); 

希望它能幫助... :)