选择器无效:使用Webdriver和Python的find_element_by_class_name不允许使用复合类名

yar*_*inc 1 xpath webdriver css-selectors python-3.x selenium-webdriver

我正在尝试通过webWhatsapp从聊天中打印我的消息之一。

我可以通过“控制台”选项卡中的Javascript完成此操作

recived_msg = document.getElementsByClassName('XELVh selectable-text invisible-space copyable-text') // returns an array of the chat
recived_msg[5].innerText // shows me the 4th message content
Run Code Online (Sandbox Code Playgroud)

问题是我试图在python上做同样的事情,但对我不起作用。

这是我尝试过的:

from selenium import webdriver
recived_msg = driver.find_element_by_class_name('XELVh selectable-text invisible-space copyable-text')
final = recived_msg[5].innerText #doesnt work for some reason
Run Code Online (Sandbox Code Playgroud)

我遇到的错误是:消息:无效的选择器:不允许使用复合类名

我对javascript有点陌生,所以很抱歉造成误会,并感谢您的帮助!:)

Deb*_*anB 5

根据selenium.webdriver.common.by实施文档:

class selenium.webdriver.common.by.By
    Set of supported locator strategies.

    CLASS_NAME = 'class name'
Run Code Online (Sandbox Code Playgroud)

所以,

  • 使用find_element_by_class_name()您将无法传递多个类名。
  • 另外,由于要返回聊天数组,因此find_element*您无需使用find_elements*

或者,您可以使用以下两种定位方法之一