Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium (with python) how to modify an element css style

I'm trying to change CSS style of an element (example: from "visibility: hidden;" to "visibility: visible;") using selenium .execute_script. (any other method through selenium+python would be accepted gracefully).

my code:

driver = webdriver.Firefox()
driver.get("http://www.example.com")

elem = driver.find_element_by_id('copy_link')

elem.execute_script(  area of my problem )

what do i need to do in order to play with the CSS of the webpage ?

like image 594
Captain_Meow_Meow Avatar asked Jul 28 '13 19:07

Captain_Meow_Meow


2 Answers

Here is an example without using any jQuery. It will hide Google's logo.

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.google.com")
driver.execute_script("document.getElementById('lga').style.display = 'none';")

The same idea could be used to show a hidden element by setting .style.display to "block", for example.

like image 104
ChrisP Avatar answered Oct 24 '22 19:10

ChrisP


Here is solution i found using document style sheets. This way is great because you can also add pseudo class styling.

script = 'document.styleSheets[0].insertRule("button:focus {background-color: red !important;}", 0 )' 
driver.execute_script(script)
like image 41
wizzfizz94 Avatar answered Oct 24 '22 21:10

wizzfizz94