首页  >  文章  >  后端开发  >  如何处理 Python Selenium 按钮定位器中的空格以避免 NoSuchElementException?

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

Susan Sarandon
Susan Sarandon原创
2024-10-22 14:41:02578浏览

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn