【问题标题】:Parsing html using Selenium - class name contains spaces使用 Selenium 解析 html - 类名包含空格
【发布时间】:2015-09-06 08:16:46
【问题描述】:

我正在尝试使用 Selenium 解析一些 html。问题是如果类名包含空格,它会引发 error

这是我要搜索的标签:<p class="p0 ng-binding">text</p>

我已经尝试了这两个选项:

result.find_element_by_class_name('departure').find_element_by_css_selector('p.p0 ng-binding').text 

result.find_element_by_class_name('departure').find_element_by_class_name('p0 ng-binding').text 

>>> selenium.common.exceptions.InvalidSelectorException: Message: The given selector p0 ng-binding is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Compound class names not permitted

谁能给我一个提示?

【问题讨论】:

  • 这是两个类名。
  • 你知道如何找到班级吗?

标签: python html parsing selenium text


【解决方案1】:

正如@eee 指出的,要检查multiple classes in a CSS selector,用点连接它们:

p.p0.ng-binding

但问题在于 ng-binding 类不是定位器的好选择。相反,只检查p0 类:

p.p0

【讨论】:

    【解决方案2】:

    p 元素有两个类:p0ng-binding

    试试这个选择器:

    find_element_by_css_selector('p.p0.ng-binding')
    

    【讨论】:

      【解决方案3】:

      这个问题不允许出现复合类名,因为类名有多个单词,你可以使用下面的 css-selectors 解决这个问题

      CssSelector("[class*='p0 ng-binding']"); //or
      CssSelector("[class='p0 ng-binding']");
      

      希望这对您有所帮助。如果您有任何疑问,请回复

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-04
        • 2018-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多