首页 > 解决方案 > 在 Selenium、python 中使用 XPath 时出错

问题描述

我正在尝试访问网站上餐厅的名称和链接。虽然我从页面源复制了 xpath,但我仍然收到错误:

NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//*[@id="component_2"]/div/div[2]/span/div[ 2]/div[1]/div/div/a"}(会话信息:chrome=78.0.3904.87)

browser.find_element_by_class_name('submit_text').click()
browser.implicitly_wait(5)
paths = []
for i in range(2,14):
    if i%6 != 0:
        paths.append(browser.find_element_by_xpath('//*[@id="component_2"]/div/div['+str(i)+']/span/div[2]/div[1]/div/div/a'))
link = []
restaurant_name = []
for i in range(10):
    element = browser.find_element_by_xpath(paths[i])
    restaurant_name.append(element.get_attribute('innerHTML'))
    link.append(element.get_attribute('href')) 


print(paths)

这是我要查找的元素的 HTML:

<a href="/Restaurant_Review-g1080422-d10195584-Reviews-Restaurant_Brau-Sant_Cugat_del_Valles_Catalonia.html" class="restaurants-list-ListCell__restaurantName--2aSdo" target="_self">
    1. Restaurant Brau
</a>

标签: pythonhtmlcssseleniumxpath

解决方案


针对您提供的页面链接运行 XPath 查询后,没有检索到任何结果。您提到您需要获取前 10 家餐厅的名称和链接。

您在循环的正确轨道上,但我会对其进行一些修改以迭代 WebElement 列表,而不是在循环中生成定位器:

# find list of all restaurant links on page
# this retrieves 24 restaurant links
restaurant_link_list = browser.find_elements_by_xpath("//div[contains(@class, 'restaurant_shelf_item')]/div/a")

# find list of all restaurant names on page
# this retrieves 24 restaurant names
restaurant_name_list = browser.find_elements_by_xpath("//div[contains(@class, 'restaurant_shelf_item')]/div/div/div[@class='item name']")

# loop through first 10 restaurants and print their name / link
for i in range(0, 10):

    # get restaurant link
    link = restaurant_link_list[i].get_attribute("href")
    print(link)

    # get restaurant name
    name = restaurant_name_list[i].get_attribute("title")
    print(name)

输出是:

https://www.tripadvisor.com/Restaurant_Review-g1080422-d10167691-Reviews-Sabatic-Sant_Cugat_del_Valles_Catalonia.html
Sabatic
https://www.tripadvisor.com/Restaurant_Review-g1080422-d7076969-Reviews-El_Vi_de_Deu-Sant_Cugat_del_Valles_Catalonia.html
El Vi de Deu
https://www.tripadvisor.com/Restaurant_Review-g1080422-d4546707-Reviews-Dakidaya-Sant_Cugat_del_Valles_Catalonia.html
Dakidaya
https://www.tripadvisor.com/Restaurant_Review-g1080422-d11892809-Reviews-Nemesis_Gastronomia-Sant_Cugat_del_Valles_Catalonia.html
Nemesis Gastronomia
https://www.tripadvisor.com/Restaurant_Review-g1080422-d3981558-Reviews-La_Pina_de_Plata-Sant_Cugat_del_Valles_Catalonia.html
La Pina de Plata
https://www.tripadvisor.com/Restaurant_Review-g1080422-d10694292-Reviews-Masia_Can_MagI-Sant_Cugat_del_Valles_Catalonia.html
Masia Can MagI
https://www.tripadvisor.com/Restaurant_Review-g1080422-d2281259-Reviews-Bocca_Restaurant_Club-Sant_Cugat_del_Valles_Catalonia.html
Bocca Restaurant & Club
https://www.tripadvisor.com/Restaurant_Review-g1080422-d10733699-Reviews-Andonie_pastissers-Sant_Cugat_del_Valles_Catalonia.html
Andonie pastissers
https://www.tripadvisor.com/Restaurant_Review-g1080422-d10195584-Reviews-Restaurant_Brau-Sant_Cugat_del_Valles_Catalonia.html
Restaurant Brau
https://www.tripadvisor.com/Restaurant_Review-g1080422-d10365477-Reviews-La_Rita-Sant_Cugat_del_Valles_Catalonia.html
La Rita

请注意,要从这样的页面读取静态内容,您可能会发现使用 Python Requests 库更有效——但是,如果您希望使用 Selenium,此代码将完成您想要做的事情。


推荐阅读