Selenium (with python) 如何修改一个元素的css样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17911980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium (with python) how to modify an element css style
提问by Captain_Meow_Meow
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).
我试图改变一个元素(例如:从CSS样式"visibility: hidden;"
来"visibility: visible;"
)使用硒.execute_script
。(通过 selenium+python 的任何其他方法都将被优雅地接受)。
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 ?
我需要做什么才能使用网页的 CSS?
回答by twil
回答by ChrisP
Here is an example without using any jQuery. It will hide Google's logo.
这是一个不使用任何 jQuery 的示例。它将隐藏 Google 的徽标。
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.
例如,通过设置.style.display
为"block"
,可以使用相同的想法来显示隐藏元素。
回答by wizzfizz94
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)