簡體   English   中英

python上的Selenium Webdriver NoSuchElementException

[英]Selenium Webdriver NoSuchElementException on python

我正在使用 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)

這里的代碼片段:(我想提取的信息在info_box = ...

for i in range(0, 195):
    search_box = driver.find_element_by_name('needle')
    if data_abril.iloc[i+1, 1] == 'N/A':
        pass
    else:
        escola = int(data_abril.iloc[i+1, 1])
        search_box.send_keys(str(escola))
        time.sleep(2)
        search_box.send_keys(Keys.DOWN)
        search_box.send_keys(Keys.RETURN)
        time.sleep(10)
        info_box = driver.find_element_by_xpath("//div[@class='widget personFields fieldsView   '][@data-state='read']")
        # info_box_a = driver.find_element_by_xpath("//div[@class='fieldsList read']")

我搜索了這個異常,主要原因之一是關於動態頁面和頁面中元素的加載時間並非全部同時發生,所以我使用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 片段


嘗試使用下面的 xpath,我們正在對類中的空格進行規范化

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

在此處輸入圖片說明

你可以更新你的 info_box 如下。

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

暫無
暫無

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

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