小编典典

选择器无效:不允许使用Selenium的复合类名称错误

selenium

我正在尝试通过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

问题是我试图在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

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

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


阅读 381

收藏
2020-06-26

共1个答案

小编典典

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

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

    CLASS_NAME = 'class name'

所以,

  • 使用find_element_by_class_name()您将无法传递多个类名。传递多个类,您将面临以下错误:

    Message: invalid selector: Compound class names not permitted
    
  • 另外,由于要返回聊天数组,因此find_element*您无需使用 find_elements*


或者,您可以使用以下两种定位策略之一:

  • CSS_SELECTOR

    recived_msg = driver.find_elements_by_css_selector(".XELVh.selectable-text.invisible-space.copyable-text")
    
  • XPATH

    recived_msg = driver.find_elements_by_xpath("//*[@class='XELVh selectable-text invisible-space copyable-text']")
    
2020-06-26