亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python 上的 Selenium Webdriver NoSuchElement

python 上的 Selenium Webdriver NoSuchElement

12345678_0001 2023-10-16 10:41:45
我正在使用 Selenium Webdriver for python,目前在 Chrome 瀏覽器上使用,并且我一直陷入這種特定庫的奇怪情況。我想在 Pipedrive 中提取一些信息(當前能夠登錄并搜索網頁內的特定線索,但是當我嘗試提取人名時出現此異常:Traceback (most recent call last):  File "C:/Users/ppbbd/PycharmProjects/fastTasks/pipeforwpp.py", line 27, in <module>    info_box = driver.find_element_by_xpath("//div[@class='widget personFields fieldsView   '][@data-state='read']")  File "C:\Users\ppbbd\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath    return self.find_element(by=By.XPATH, value=xpath)  File "C:\Users\ppbbd\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element    'value': value})['value']  File "C:\Users\ppbbd\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute    self.error_handler.check_response(response)  File "C:\Users\ppbbd\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response    raise exception_class(message, screen, stacktrace)selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='widget personFields fieldsView   '][@data-state='read']"}  (Session info: chrome=80.0.3987.149)我搜索了這個異常,主要原因之一是動態頁面和頁面中元素的加載時間并不全部同時,所以我只是用來測試time.sleep(10)是否是這種情況,但我仍然收到上面顯示的錯誤。重要提示:這是 html 的一個片段,如果您查看代碼末尾,您會發現注釋 info_box_a = driver.find_element_by_xpath("//div[@class='fieldsList read']")工作正常,并且正如您在圖像中看到的那樣,這個特定的 div 位于有問題的 div 內,所以 Selenium 怎么可能無法找到父級但其子級? HTML 片段頁面來源:我無法將其發布在這里,因為它會繞過 30k 字符限制,并且源頁面位于登錄區域內,因此即使我發布網址,除非您在 Pipedrive 上有帳戶,您也看不到頁面源(如果我錯了請糾正我)。任何顯示另一個 html 片段的建議我可以更新問題。更新:正如您在下面的鏈接中看到的,一個人的電話類別值的具體信息class="item read    phoneField editable    "上有這么多空格,這是否會妨礙查找該元素?或者class="item read phoneField editable"意思完全一樣嗎?重要的 HTML 片段
查看完整描述

1 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

嘗試使用下面的 xpath,我們將類中的空格規范化。

//div[normalize-space(@class)='widget?personFields?fieldsView'][@data-state='read']

https://img1.sycdn.imooc.com/653e755d0001ecc805000223.jpgz

您可以按如下方式更新您的 info_box。

info_box?=?driver.find_element_by_xpath("http://div[normalize-space(@class)='widget?personFields?fieldsView'][@data-state='read']")


查看完整回答
反對 回復 2023-10-16
  • 1 回答
  • 0 關注
  • 125 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號