簡體   English   中英

帶xpath的Python Selenium的select元素時出現InvalidSelectorException

[英]InvalidSelectorException when select element for Python Selenium with xpath

我有這個html片段:

<div class="btn">
    <a href="javascript:void(0)" class="S_btn_b" node-type="OK">
        <span>確定</span>
    </a>
</div>

我想選擇確定元素並單擊它。 我用xpath嘗試了以下選擇器:

confirm_btn = driver.find_element_by_xpath('//div[@class="btn"]//span[text()="確定"]')

但是程序只是抱怨說這不是一個帶有此異常的有效選擇器:

Traceback (most recent call last):
  File "test.py", line 63, in <module>
    ac.move_to_element(driver.find_element_by_xpath('//div[@class="btn"]/span[text()="確定"'))
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 230, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 662, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException

我做錯什么了嗎 ?

如果未將XPath表達式標記為Unicode,則在將搜索命令發送到瀏覽器時,漢字會被弄亂,並且搜索將搜索您想要的內容以外的其他內容,因此將XPath表達式標記為Unicode:

confirm_btn = driver.find_element_by_xpath(u'//div[@class="btn"]//span[text()="確定"]')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM