【问题标题】:Xpath to get Background-image attribute in DIVXpath 在 DIV 中获取背景图像属性
【发布时间】:2013-09-20 05:17:37
【问题描述】:

在 (selenium webdriver) 中提及 ID 的 DIV 标签的 background-image CSS 属性的 Xpath 可以是什么?

例如: (div id="abc", style="width: 538px !important; height: 242px !important; 背景图片: url(http://test.com/images/abc.png); 位置: 相对; 背景位置: 0% 0 %;")

我想用 url 找到这张图片:(http://test.com/images/abc.png)

【问题讨论】:

    标签: xpath selenium webdriver selenium-webdriver


    【解决方案1】:

    我不明白你的问题,请格式化并扩展一下,这对我来说有点模糊。

    我假设您想问以下问题之一,但不确定是哪一个。

    • 如何使用 Selenium 获取 divbackground-image CSS 属性?

    答案:使用 Selenium 的原生 GetCssValue() (C#)、css_value (Ruby) 或其他语言绑定中的等效方法。

    IWebElement abc = driver.FindElement(By.XPath("//[@id='abc']")); // use XPath as you requested
    string imageUrl = abc.GetCssValue("background-image");
    
    • 如何使用 XPath 通过 background-image CSS 属性定位 div 元素?

    答案:如果您不想使用 ID(在本例中应该这样做),您完全可以在 CssSelector 或 XPath 中使用。 (XPath 是您应该选择的最后一个选项)

    IWebElement abc = driver.FindElement(By.XPath("//div[contains(@style, 'background-image: url(http://test.com/images/abc.png);')]"));
    
    • 如何使用XPath获取元素属性内容background-image

    答案:对 Selenium 不是很有用,但在这里你有它://div[@id='abc']/@style

    【讨论】:

    • 我认为他想获取图像本身而不是 url
    猜你喜欢
    • 2021-04-22
    • 1970-01-01
    • 2012-07-18
    • 2020-01-04
    • 2013-08-31
    • 2019-02-19
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多