【发布时间】:2018-05-27 07:08:01
【问题描述】:
我有一个脚本,它循环遍历标题的一系列元素,然后单击“下一步”以加载下一系列要抓取的元素。问题是第 2 页和很可能在第 3 页上的“下一步”按钮更改的 xpath。我不能使用按类查找或按 ID 查找,因为这些类/ID 名称出现不止一次。一旦在第二页找不到“下一步”按钮,脚本就会出错。
while True:
deal_title = browser.find_elements_by_xpath("//a[@id='dealTitle']/span")
titles = []
for title in deal_title:
titles.append(title.text)
deal_link = browser.find_elements_by_xpath("//div[@class='a-row dealDetailContainer']/div/a[@id='dealTitle']")
links = []
for link in deal_link:
links.append(link.get_attribute('href'))
deal_image = browser.find_elements_by_xpath("//a[@id='dealImage']/div/div/div/img")
images = []
for image in deal_image:
images.append(image.get_attribute('src'))
deal_price = browser.find_elements_by_xpath("//div[@class='a-row priceBlock unitLineHeight']/span")
prices = []
for price in deal_price:
prices.append(price.text)
try:
#clicks next button - this is the xpath for the page 1 button
browser.find_element_by_xpath("//span[@class='a-declarative']/div[2]/ul/li[@class='a-last']/a").click()
except NoSuchElementException:
break
下面是第 2 页上下一步按钮的 xpath:
browser.find_element_by_xpath("//span[@class='a-declarative']/div[1]/ul/li[@class='a-last']/a").click()
【问题讨论】: