不允许使用硒化合物类名称 [英] Selenium Compound class names not permitted
问题描述
我有下面的代码,点击一个元素弹出一个屏幕并复制其中的文本
el1 = driver.find_element_by_id("keyDev-A")el1.click()el2 = driver.find_element_by_class_name(内容")打印(el2.text)
但是,当我试图让 selenium
用
el3 = driver.find_element(By.CLASS_NAME, "action-btn cancel alert-display")el3.click()
它会产生一条错误消息:
无效的选择器:不允许复合类名
这是我试图让 selenium
点击的 HTML.关闭
按钮.
我应该如何编写 el3
才能点击 关闭 按钮?
解决方案 Leon's注释 导致不再支持复合类名称的正确信息.你可以做的是尝试使用 css 选择器.在您的情况下,以下代码行应该可以帮助您获得所需的元素:
el3 = driver.find_element_by_css_selector(".action-btn.cancel.alert-display")
它在 class 属性中找到具有所有三个类(action-btn、cancel 和 alert-display)的元素.请注意,类的顺序在这里无关紧要,任何类都可能出现在类属性中的任何位置.只要元素具有所有三个类,它就会被选中.如果要固定类的顺序,可以使用以下 xpath :
el3 = driver.find_element_by_xpath("//*[@class='action-btn cancel alert-display']")
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
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>
How should I be writing el3
in order to click on the Close button?
解决方案 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")
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屋!
查看全文
相关文章
- 硒化合物类名称不允许;
- 不允许使用限定名称;
- 重复的名称不允许;
- 不允许使用 Selenium 复合类名称;
- 对象名称中不允许使用``/''字符;
- 通过覆盖抗锯齿化合物?;
- 此时元素div上不允许使用属性名称;
- 不允许输入类型名称(不能使用重载);
- 错误:C++ 中不允许使用类型名称;
- 此时元素元素上不允许使用属性名称;
- 使用网站名称时不允许使用 WCF 服务方法;
- 不允许eval();
- 不允许操作......;
- UseCleartextTraffic不允许;
- 不允许使用内部包装;
- Laravel - @yield()不允许使用if;
- 不允许用户使用指令;
- 不允许使用子节点.;
- 不允许的复合类名称错误Webdriver;
- WCF路由服务:filterTable中不允许使用属性“名称”;
- “此时在元素div上不允许使用属性名称";
- 如何修复错误名称:不允许指定的名称:_destroy?;
- 不允许的方法 请求的 URL 不允许使用该方法.405错误;
- 不允许的方法 请求的 URL 不允许使用该方法.405错误;
- 不允许使用 CORS 405 方法;
Python最新文章
- 类型错误:只有长度为1的阵列可以尝试拟合指数的数据转换到Python标量;
- bs4.FeatureNotFound:找不到一棵树建设者您所要求的功能:LXML。你需要安装一个解析器库?;
- 系列的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all();
- (unicode错误)'unicodeescape'编解码器无法解码位置2-3中的字节:truncated \UXXXXXXXX escape;
- 将pandas dataframe中的列从int转换为string;
- Python:由实例对象调用方法:“missing 1 required positional argument:'self'”;
- Sparksql过滤与多个条件(与where子句中选择);
- JSONDe codeError:期待值:1行1列(CHAR 0);
- Cmake不能找到Python库;
- Python - 将Dataframe中的所有项目转换为字符串;