我正在尝试使用Selenium WebDriver在Python中获取后台url属性。使用' color'和'位置'属性但没有背景'或者' background-url'。
home = driver.find_element_by_id("link-home")
print(home.value_of_css_property("position"))
=> absolute
任何人都可以帮助我吗?
这是css:
#main-header a#link-home {
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB8AAAATCAYAAABobNZCAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAOFJREFUeNpiPLRnNQMR4D8QM2KTsHUOYSAXMBFpMTKNLP6fgQLARKTFjDgcwEAry5Et5qeFA5iIsNgDiO9Baao6gImAxX5AvBGIhaC0H66ERw3LkS0OB2JQVmCDirFB+eHUcgATDosTgHgpksUMSA5YCpUHO+Dw3jX/KbUc2eI0IJ4PxMxQPgjPQmIzQ+XTKHUAE5rFOUA8E0l+JhJ/Jha5fGAhQ7YDGJFKuGogbkGXR7IoHUdKrwE6oJXSOG/BkfL/Q4MYV4nWQsvilWaAhVC04CkHaFq80hyMWj7yLAcIMADiujmyHco+MAAAAABJRU5ErkJggg==") no-repeat scroll 0px 0px transparent;
left: 10px;
padding-left: 23px;
position: absolute;
top: -102px;
color: #DFEFFC;
}
答案 0 :(得分:1)
我已经在 firefox 中使用background
或background-url
属性(使用firefox 35.0.1和selenium 2.45)重现了这个问题。
在Firefox中对我有用的解决方法:
print driver.execute_script("""
var element = arguments[0],
style = element.currentStyle || window.getComputedStyle(element, false);
return style['background-image'];
""", body)
演示:
>>> from selenium import webdriver
>>>
>>> driver = webdriver.Firefox()
>>> driver.get("http://www.w3schools.com/cssref/tryit.asp?filename=trycss_background")
>>>
>>> driver.switch_to.frame("iframeResult")
>>>
>>> body = driver.find_element_by_tag_name("body")
>>>
>>> print driver.execute_script("""
... var element = arguments[0],
... style = element.currentStyle || window.getComputedStyle(element, false);
...
... return style['background-image'];
... """, body)
url("http://www.w3schools.com/cssref/smiley.gif")
或者,只需切换到webdriver.Chrome()
。