硒化合物类名称不允许 [英] Selenium Compound class names not permitted

查看:59
本文介绍了硒化合物类名称不允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,单击一个元素以弹出一个屏幕并在其中复制文本

I have the below code that clicks on an element to pop up a screen and copy the text in it

el1 = driver.find_element_by_id("keyDev-A")
el1.click()
el2 = driver.find_element_by_class_name("content")
print(el2.text)

但是,当我尝试使用

硒来单击该弹出窗口中的按钮时,

However, when I tried to get selenium to click on the button within that popup with

el3 = driver.find_element(By.CLASS_NAME, "action-btn cancel alert-display")
el3.click()

它会产生一条错误消息:

It produces an error message:

invalid selector: Compound class names not permitted

这是我试图获取 selenium 所单击的HTML.关闭按钮.

This is the HTML that I am trying to get selenium to click on. The Close button.

<div class="nav">
    <span class="action-btn confirm prompt-display">Confirm</span>
    <span class="action-btn cancel prompt-display">Cancel</span>
    <span class="action-btn cancel alert-display">Close</span>
</div>

我应该如何编写 el3 以便单击关闭按钮?

How should I be writing el3 in order to click on the Close button?

推荐答案

Leon'scomment 会导致正确的信息,即不再支持复合类名称.您可以做的是尝试使用CSS选择器.在您的情况下,以下代码行应该可以帮助您获取所需的元素:

Leon's comment leads to the correct information that compound class names are no longer supported. What you could do instead is try using css selectors. In your case, the following line of code should help you get the element you want :

el3 = driver.find_element_by_css_selector(".action-btn.cancel.alert-display")

它将在class属性中找到具有所有三个类(action-btn,cancel和alert-display)的元素.请注意,类的顺序在这里无关紧要,任何类都可以出现在class属性中的任何位置.只要元素具有所有三个类,就将其选中.如果要固定类的顺序,则可以使用以下xpath:

It finds the element with all three classes (action-btn, cancel and alert-display) in the class attribute. Do note that the order of the classes does not matter here and any of the classes may appear anywhere in the class attribute. As long as the element has all three classes, it will be selected. If you want the order of the classes to be fixed, you can use the following xpath :

el3 = driver.find_element_by_xpath("//*[@class='action-btn cancel alert-display']") 

这篇关于硒化合物类名称不允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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