使用 selenium python 使用化合物 class 解析 HTML 内容

Parsing the HTML content using compound class using selenium python

我的 GUI 中有一个显示按钮,用于显示连接状态(带绿色勾号的按钮表示连接已建立,带红叉号的按钮表示未连接) 我必须使用我的代码检查状态。 我正在解析那个特定 title-bar class 名称 (container-fluid) 的内容。 从这里,我正在解析那个显示按钮的显式内容。

elem = driver.find_element_by_class_name("container-fluid")
a= elem.get_attribute("outerHTML")
b= a.split("powerOn icon-ok-sign") 

在此之后,我解析了该按钮的一些显式内容并确定连接是否存在。

但是如果我使用 class="powerOn icon-ok-sign",我会得到错误:

Compound class names not permitted

<div class="powerOn icon-ok-sign" data-original-title="Connection" style=" font-size: 2em;" data-toggle="tooltip" title="" data-placement="bottom" ng-class="{&quot;powerOn icon-ok-sign&quot;: titleArea.systemStatus.connection.value, &quot;powerOff icon-remove-sign&quot; : !titleArea.systemStatus.connection.value}"></div>

But If I use class="powerOn icon-ok-sign", I get error Compound class names not permitted

实际上名字。

您应该尝试使用 on of then 代替 :-

driver.find_element_by_class_name("powerOn")

driver.find_element_by_class_name("icon-ok-sign")

或者使用 css_selector 使用多个 class 名称定位相同元素的最佳方法:-

driver.find_element_by_css_selector(".powerOn.icon-ok-sign")

参考link:-

  • Compound class names are not supported error in WebDriver
  • How to avoid Compound Class name error in Page Object?

您可以改用 CSS Selector 搜索:

driver.find_element_by_css_selector(".powerOn.icon-ok-sign")

或将 class 名称之一用于 select 您的元素:

driver.find_element_by_class_name("powerOn")