【问题标题】:problem: Getting to the element by using CSS selectors attribute 'style' with selenium问题:通过使用带有 selenium 的 CSS 选择器属性“样式”来获取元素
【发布时间】:2011-09-08 16:29:28
【问题描述】:

我在使用带有 selenium 的“样式”属性访问元素时遇到问题。问题是使用 xpath 选择器我可以做到:

int(self.selenium.get_element_index("//div[contains(@class,'%s')][contains(@style,'%s')][contains(@style,'%s')]"%(pin_class_name,map_object_position[0],map_object_position[1])))

^ 这会返回结果

但是当我尝试使用 css 选择器获取元素时,似乎找不到这样的元素:

int(self.selenium.get_element_index("css=div[class*='%s'][style*='%s'][style*='%s']"%(pin_class_name,map_object_position[0],map_object_position[1])))

有没有人遇到过这个问题,或者知道是否可以使用 css 选择器使用属性“样式”进行导航。我描述这个问题,是因为我想对这两种方法的使用做一个比较(我怀疑xpath的使用不是应该的)

【问题讨论】:

    标签: python xpath selenium css-selectors


    【解决方案1】:

    我进行了广泛的实验比较和对比 Selenium 的 XPath、CSS 和 DOM 定位器,发现虽然可以从 XPath 或 DOM 访问 style 属性,但一个 不能 从 CSS(就像你猜到了)。您可以在我最近发布在 Simple-Talk.com 上的快速参考图表XPath, CSS, DOM and Selenium: The Rosetta Stone 中找到该花絮(参见脚注 2)以及我的完整分析。

    【讨论】:

      【解决方案2】:

      *= 运算符不在 CSS2 中,因此根据您使用的 Selenium 版本,它可能对您不可用。

      【讨论】:

        猜你喜欢
        • 2015-10-15
        • 2012-06-01
        • 1970-01-01
        • 2012-03-14
        • 1970-01-01
        • 2021-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多