Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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 ?

share|improve this question

2 Answers 2

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.

share|improve this answer
    
thanks, i have tried it only to receive the following error: WebDriverException: Message: u'document.getElementById(...) is null' ; –  user2627775 Jul 28 '13 at 21:04
    
@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. –  ChrisP Jul 29 '13 at 2:25
    
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 Jul 29 '13 at 9:44
    
@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. –  ChrisP Jul 29 '13 at 12:37

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');")
share|improve this answer
    
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'... –  user2627775 Jul 28 '13 at 21:06
    
what exactly is the copy link?? –  Abhyudaya Srinet Dec 28 '14 at 10:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.