Python - 无法通过 class_name 定位元素但能够通过 XPATH 定位

Python - Unable to locate an element by class_name but able to locate by XPATH

我试图通过 class_name 定位一个元素,但我收到错误“没有这样的元素”
当我试图通过 XPATH 定位相同的元素时,它有效

为什么他的class_name找不到元素?

这是我试过的 2 个定位器:
1: txt_company_name = (By.CLASS_NAME, 'crm-entity-widget-content-input crm-entity-widget-content-search-input')

2: txt_company_name = (By.XPATH, '//*[@class="crm-entity-widget-content-input crm-entity-widget-content-search-input"]')

这是HTML:

<input type="text" placeholder="Company name, phone or email" class="crm-entity-widget-content-input crm-entity-widget-content-search-input" autocomplete="nope">

原因是 CLASS_NAME 和 space 在 Selenium 中不起作用。

基本上,如果您在 HTML 中看到这样的 class 名称:

crm-entity-widget-content-input crm-entity-widget-content-search-input

就是这个classcrm-entity-widget-content-inputcrm-entity-widget-content-search-input

的组合

所以你要么必须使用下面的 CSS_SELECTOR

input.crm-entity-widget-content-input

input.crm-entity-widget-content-search-input

甚至下面的应该工作,只要它在 HTMLDOM:

中是唯一的
.crm-entity-widget-content-input.crm-entity-widget-content-search-input

基本上,我们用.表示CSS中的class,所以你可以去掉space,放一个.,也可以在开始,这应该可以帮助您找到正确的 CSS.

或您拥有的 XPath 就足够了。但是,CSS 比 XPath 具有更高的优先级。