我正在使用 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 片段
python 上的 Selenium Webdriver NoSuchElement
12345678_0001
2023-10-16 10:41:45