22

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 ?

4 Answers 4

29

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, i have tried it only to receive the following error: WebDriverException: Message: u'document.getElementById(...) is null' ;
@user2627775, Can you confirm that the element actually exists and is not wrapped in an <iframe>? Look at driver.page_source to see whether the element you wish to show or hide is actually present in the DOM. Also, ensuring that you have the latest version of selenium might be worthwhile.
selenium web driver 2.33, and through driver.page_source i did confirm that the element is in the source. (id did: html_source = browser.page_source if "whatever" in html_source: print 'in'. the problematic html is not in <iframe>, it's in a div/div/a/span. could 'bindpoint = "copy_link_wraper"' be the problem? (as in stackoverflow.com/questions/4607582/…)
@user2627775, You'll have to edit your post with the website you are trying to scrape and the ID of the div so that we can actually run your code. There is nothing else I can say without seeing the problem myself.
10

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)

Comments

5

String in execute_script() is JS code you want to run (docs).

If you use jQuery it can be just

driver.execute_script("$('#copy_link').css('visibility', 'visible');")

2 Comments

i have tried, got WebDriverException: Message: u'<![EX[["Tried to get element with id of \\"%s\\" but it is not present on the page.","#copy_link"]]]>' ; Stacktrace:. it appears that the element is only activated (or visible) on a 'hover mouse'...
what exactly is the copy link??
0

If you catch the element by its xpath, then you can change or add styles to it as

location_selector = driver.find_element(By.XPATH, '//select[@data-placeholder="Return Locations"]')
driver.execute_script("arguments[0].style.display = 'block';", location_selector)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.