Anon0441
Anon0441

Reputation: 67

Using selenium to go to the next page on Amazon

I have been wondering how I could see when I am at the last page on an amazon listing. I have tried to get at the last page number on the bottom of the screen with nothing working, so I tried a different approach. To see if the 'Next' button can still be clicked. Here is what i have so far, any ideas on why it wont go to the next page?

from selenium.webdriver.chrome.webdriver import WebDriver
from selenium import webdriver
import time

def next():
    giveawayPage = 1
    currentPageURL = 'https://www.amazon.com/ga/giveaways?pageId=' + str(giveawayPage)

while True:
    try:
        nextButton = driver.find_element_by_xpath('//*[@id="giveawayListingPagination"]/ul/li[7]')
    except:
        nextPageStatus = 'false'
        print('false')
    else:
        nextpageStatus = 'true'
        giveawayPage = giveawayPage + 1
        driver.get(currentPageURL)
    if (nextPageStatus == 'false'):
        break




if __name__ == '__main__':
    driver = webdriver.Chrome('./chromedriver')
    driver.get('https://www.amazon.com/ga/giveaways?pageId=1')
    next()

Upvotes: 0

Views: 1040

Answers (2)

ewwink
ewwink

Reputation: 19154

the li for Next button is always exist, there are several ways to check if it last page:

  • check if the li has multiple class or not only a-last but also has class a-disabled

    //li[@class="a-last"]
    
  • check if the <a> element exist in the li

    //*[@id="giveawayListingPagination"]/ul/li[7]/a
    

Upvotes: 0

anderw
anderw

Reputation: 103

The reason this doesn't work is because if you go to the last page of an Amazon Giveaway, the element that you're selecting is still there, it's just not clickable. On most pages, the element looks like:

<li class="a-last">...</li>

On the last page, it looks instead like:

<li class="a-disabled a-last">...</li>

So rather than checking if the element exists, it might be better to check if the element has the class 'a-disabled'.

Upvotes: 1

Related Questions