not_this_again not_this_again - 4 months ago 25
Python Question

Modifying a style attribute in Selenium with execute_script, but the attribute's value doesn't change

Using: Selenium with PhantomJS in Python

I need to set a style attribute of an input tag to '' because it is set to "display:None" which prevents me from filling the input with send_keys in Selenium.

I am using execute_script to achieve this. execute_script runs, but the style attribute remains unaltered. Why isn't PhantomJS changing the style attribute?

HTML with style attribute I want to remove:

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


Python Selenium script:

Why isn't the style attribute's value being changed by 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;

Answer

Try as below :-

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");

Hope it helps...:)