使用python,如何使用selenium获取背景图像?哪一个有内联CSS?我想从后台获取图像URL : url()
<div id="pic" class="pic" data-type="image" style=" background-image: url(http://test.com/images/image.png;); height: 306px; background-size: cover;"></div>
发布于 2019-11-29 11:05:37
要获得背景图像,您需要使用value_of_css_property(property_name)
方法,并且必须为visibility_of_element_located()
导出WebDriverWait,您可以使用以下任何一个Locator Strategies
使用
CSS_SELECTOR
:导入re my_property =WebDriverWait(驱动程序,my_property)1)
XPATH
):导入re my_property =WebDriverWait(驱动程序,my_property)1)
test.com/images/image.png
更新
当url以双引号(即"..."
)结束时,您可以使用以下解决方案:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='pic' and @id='pic'][@data-type='image']"))).value_of_css_property("background-image").split('"')[1])
参考文献
你可以找到一些与以下相关的讨论:
中的子字符串
发布于 2019-11-29 11:48:06
试试这个:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.pic#pic[data-type='image']"))).get_attribute("href"))
发布于 2019-11-29 12:20:17
要获得URL
,您需要使用regular expression
。为了使用正则表达式Import re
,然后使用以下正则表达式。
import re
itemimage=WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div#pic"))).value_of_css_property("background-image")
print(re.search("(?P<url>http?://[^\s;]+)", itemimage).group("url"))
输出
http://test.com/images/image.png
https://stackoverflow.com/questions/59103063
复制相似问题