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

前端 未结 3 1746
甜味超标
甜味超标 2020-12-31 04:17

I\'m trying to change CSS style of an element (example: from \"visibility: hidden;\" to \"visibility: visible;\") using selenium .execute_scr

相关标签:
3条回答
  • 2020-12-31 04:53

    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.

    0 讨论(0)
  • 2020-12-31 04:57

    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)
    
    0 讨论(0)
  • 2020-12-31 05:13

    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');")
    
    0 讨论(0)
提交回复
热议问题