无效的选择器:使用 Selenium 时不允许复合类名错误 [英] Invalid selector: Compound class names not permitted error using 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! :)
推荐答案
根据 selenium.webdriver.common.by
实现:
class selenium.webdriver.common.by.By
Set of supported locator strategies.
CLASS_NAME = 'class name'
所以,
使用
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*
代替 find_element*
>
作为替代,您可以使用以下任一定位器策略:
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屋!