【问题标题】:Next Button XPath Changes on Page #2, Won't Loop第 2 页上的下一个按钮 XPath 更改,不会循环
【发布时间】: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()

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    尝试替换

    browser.find_element_by_xpath("//span[@class='a-declarative']/div[2]/ul/li[@class='a-last']/a").click()
    

    browser.find_element_by_link_text("Next→").click()
    

    【讨论】:

    • 你能看到我最新的问题吗?希望你能帮我回答吗?顺便说一句,我在基辅有一个朋友。
    【解决方案2】:

    您可以将它们组合在一起 browser.find_element_by_xpath("//span[@class='a-declarative']/div[2]/ul/li[@class='a-last']/a|//span[@class='a-声明式']/div[1]/ul/li[@class='a-last']/a “)。点击() 或者你可以写一个更通用的 xpath //li[@class='a-last']/a

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 1970-01-01
      • 2018-12-31
      相关资源
      最近更新 更多