首页 > 解决方案 > 如何获取产品名称的 xpath 作为超链接

问题描述

我正在尝试从以下链接获取数据抓取:

https://www.amazon.co.uk/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=887961316995

无法获取产品名称的 xpath(您可以通过单击 url 进行检查)。因为它是超链接,所以没有在self.hasxapth中编写什么代码?

if self.hasXpath('//[@id="result_0"]/div/div/div/div[2]/div[1]/div[1]/a'):
           self.browser.find_element_by_link_text("Barbie Star Light Adventure Galaxy Dog Figure, Pink").click()
            if self.hasXpath("//*[@id='productDescription']"):
                description_list = self.browser.find_elements_by_xpath("//*[@id='productDescription']")

                description = []
                for curr_description in description_list:
                    if curr_description.text.encode('ascii','ignore').strip()!="":
                        description.append(curr_description.text.encode('ascii','ignore').strip())
                description_string = "|".join(description)

            product_dict['Product Description']=description_string
            print product_dict

标签: python-2.7seleniumxpath

解决方案


通过使用以下 xpath,您可以获得产品名称。如果找到多个元素,则创建一个元素列表,然后获取每个元素的文本

xpath : "//ul[@id='s-results-list-atf']//a[contains(@class,'s-access-detail-page')]"


推荐阅读