选择器无效:使用Selenium时,不允许使用复合类名称错误 [英] Invalid selector: Compound class names not permitted error using Selenium

查看:223
本文介绍了选择器无效:使用Selenium时,不允许使用复合类名称错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to print one of my messages from a chat via webWhatsapp.

我能够通过控制台"选项卡中的Javascript完成此操作

I was able to do it via Javascript from the Console tab i did it this way

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上做同样的事情,但对我不起作用.

The problem is that i tried to do same thing on python but it doesnt work for me..

这里是我尝试过的:

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

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

my Error that i'm getting is: Message: invalid selector: Compound class names not permitted

我对javascript还是很陌生,因此对您的误会表示歉意,并感谢您的帮助! :)

I'm kinda new to javascript so sorry for missunderstanding and thank you for your help! :)

推荐答案

根据所以

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

  • Using find_element_by_class_name() you won't be able to pass multiple class names. Passing multiple classes you will face the error as:

Message: invalid selector: Compound class names not permitted

  • 此外,由于您希望返回聊天数组,因此需要使用 find_elements*

  • Additionally, as you want to return an array of the chats, so instead of find_element* you need to use find_elements*

    您也可以使用以下任一方法:定位器策略:

    As an alternative you can use either of :the following Locator Strategies:

    • 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']")
    

  • 这篇关于选择器无效:使用Selenium时,不允许使用复合类名称错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆