如何处理 Python Selenium 按钮定位器中的空格以避免 NoSuchElementException?

Susan Sarandon
发布: 2024-10-22 14:41:02
原创
587 人浏览过

How to Handle Spaces in Python Selenium Button Locators to Avoid NoSuchElementException?

如何在 Python Selenium 按钮定位器中处理空格字符

当尝试单击 Python Selenium 中的按钮时,构建 CSS 非常重要正确选择器以避免遇到 NoSuchElementException。考虑以下 HTML 结构:

<code class="html"><div class="b_div">
    <div class="button c_button s_button" onclick="submitForm('mTF')">
        <input class="very_small" type="button"/>
        <div class="s_image"></div>
        <span>
           Search
        </span>
    </div>
</div></code>
登录后复制

要单击“搜索”按钮,错误的尝试可能是:

<code class="python">driver.find_element_by_css_selector('.button .c_button .s_button').click()</code>
登录后复制

这会导致异常,因为有空格每个类名之间。要解决此问题,请删除空格:

<code class="python">driver.find_element_by_css_selector('.button.c_button.s_button').click()</code>
登录后复制
登录后复制

同样,单击“重置”按钮:

<code class="python">driver.find_element_by_css_selector('.button.c_button.s_button').click()</code>
登录后复制
登录后复制

了解如何处理 CSS 选择器中的空格字符对于成功至关重要Python Selenium 中的元素位置和交互。

以上是如何处理 Python Selenium 按钮定位器中的空格以避免 NoSuchElementException?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!