簡體   English   中英

Selenium Web 抓取 find_element_by_xpath 錯誤

[英]Selenium Web Scraping find_element_by_xpath error

我正在嘗試從https://www.mahindrausa.com/map-hours-directions-tractors-utvs-farming-equipment--使用 python 和 selenium 庫的經銷商 -- 定位經銷商,但我無法使用“find_element_by_xpath”命令提取文本。

每當我運行下面的程序時,它都會給我一個帶有錯誤的空白文本,我不確定我在這里做錯了什么。 下面是錯誤

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="locationsAR"]/div/ul/li[1]/a[2]"}
  (Session info: chrome=83.0.4103.116)

有人可以幫忙嗎?

from selenium import webdriver
import pandas as pd

data={}
abvr=['AL','AR','AZ','CA','CO','CT','DE','FL','GA','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NH','NJ','NM','NV','NY','OH','OK','OR','PA','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY']
df=pd.DataFrame(columns=['Name','Address 1','Address 2','Phone#','Email'])
path=r"C:\Program Files\chromedriver.exe"
driver=webdriver.Chrome(path)
driver.get("https://www.mahindrausa.com/map-hours-directions-tractors-utvs-farming-equipment--dealership--locate-a-dealer")
a=driver.find_elements_by_class_name("locations-list")
for s in abvr:
    name="locations"+s
    for n in a:
        for k in n.find_elements_by_class_name("state-location"):
            count=1
            names= '//*[@id=\"'+name+'\"]/div/ul/li['+str(count)+']/h4'
            address1='//*[@id=\"'+name+'\"]/div/ul/li['+str(count)+']/span[1]/span[1]'
            address2='//*[@id=\"'+name+'\"]/div/ul/li['+str(count)+']/span[1]/span[2]'
            phone='//*[@id=\"'+name+'\"]/div/ul/li['+str(count)+']/span[2]/a'
            email='//*[@id=\"'+name+'\"]/div/ul/li['+str(count)+']/a[2]'
            data['Name']=k.find_element_by_xpath(names).text
            data['Address 1']=k.find_element_by_xpath(address1).text
            data['Address 2']=k.find_element_by_xpath(address2).text
            data['Phone#']=k.find_element_by_xpath(phone).text
            data['Email']=k.find_element_by_xpath(email).text
            df=df.append(data,ignore_index=True)
            count=+1
driver.quit()
print(df)

您的 Xpath 非常不穩定。 我嘗試使用//*[@id="locationsAR"]/div/ul/li[1]/a[2] xpath 找到一個元素,但沒有找到,但我找到了//*[@id="locationsAR"]/div/ul/li[1]/a

那是因為您試圖為亞利桑那州的第一個位置獲取 email 並且它是空白的:該 xpath 沒有元素。

好吧,如果您想收集經銷商列表,可能有一個更簡單的解決方案。 完整列表(498 個經銷商)似乎存儲在一個名為jCollection的變量中,您可以使用以下代碼讀取該變量:

from selenium import webdriver
driver=webdriver.Chrome()
driver.get("https://www.mahindrausa.com/map-hours-directions-tractors-utvs-farming-equipment--dealership--locate-a-dealer")
dealers = driver.execute_script("return jCollection;")
print(dealers)

Output

{'features': [{'geometry': {'coordinates': [36.16876, -84.07945],
    'type': 'Point'},
   'properties': {'address': '2401 N Charles G Seivers Blvd',
    'city': 'Clinton',
    'dealerCode': 'TOM08',
    'dealerName': " Tommy's Motorsports",
    ...
    }}]}

暫無
暫無

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

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