首頁  >  文章  >  後端開發  >  如何處理 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