Python 不允许使用 Selenium 复合类名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37771604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium Compound class names not permitted
提问by jake wong
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
但是,当我尝试selenium
点击该弹出窗口中的按钮时
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
它会产生一条错误消息: invalid selector: Compound class names not permitted
This is the HTML that I am trying to get selenium
to click on. The Close
button.
这是我试图selenium
点击的 HTML 。该Close
按钮。
<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>
How should I be writing el3
in order to click on the close button?
我应该怎么写el3
才能点击关闭按钮?
回答by sagarwadhwa1
Leon's commentleads 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 :
Leon 的评论导致了正确的信息,即不再支持复合类名。你可以做的是尝试使用 css 选择器。在您的情况下,以下代码行应该可以帮助您获得所需的元素:
el3 = driver.find_element_by_css_selector(".action-btn.cancel.alert-display")
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 :
它在 class 属性中找到具有所有三个类(action-btn、cancel 和 alert-display)的元素。请注意,类的顺序在这里无关紧要,任何类都可能出现在类属性中的任何位置。只要元素具有所有三个类,它就会被选中。如果要固定类的顺序,可以使用以下 xpath :
el3 = driver.find_element_by_xpath("//*[@class='action-btn cancel alert-display']")
回答by William Tong
I am late to this question. But I also found a work around by treating the compound classes as a String, using tag_name, and get_attribute('class'), when you are not familiar with Xpath. It needs some more lines of code but it's straight forward and fit for beginners like me.
这个问题我迟到了。但是,当您不熟悉 Xpath 时,我也找到了一种解决方法,即使用 tag_name 和 get_attribute('class') 将复合类视为字符串。它需要更多的代码行,但它很简单,适合像我这样的初学者。
elements = driver.find_elements_by_tag_name('Tag Name Here')
for element in elments:
className = watchingTable.get_attribute('class')
print(className)
if className == 'Your Needed Classname':
#Do your things