Skip to content Skip to sidebar Skip to footer

Accessing The Next Page Using Selenium

First, I have never used selenium until yesterday. I was able to scrape the target table correctly after many attempts. I am currently trying to scrape the tables on sequential

Solution 1:

You can use the below logic.

lastPage = WebDriverWait(driver,120).until(EC.element_to_be_clickable((By.XPATH,"(//ul[starts-with(@class,'pagination hidden-xs ng-scope')]/li[starts-with(@ng-repeat,'pageNumber')])[last()]")))
driver.find_element_by_css_selector("i.web-icon-plus").click()
pages = lastPage.text
pages = '5'for pNumber in range(1,int(pages)):
    currentPage = WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//ul[starts-with(@class,'pagination hidden-xs ng-scope')]//a[.='" + str(pNumber) + "']")))
    print ("===============================================")
    print("Current Page : " + currentPage.text)
    currentPage.location_once_scrolled_into_view
    currentPage.click()
    WebDriverWait(driver,120).until_not(EC.element_to_be_clickable((By.CSS_SELECTOR,"#loading")))
    # print rows data here
    rows = driver.find_elements_by_xpath("//table[starts-with(@class,'cve-table')]/tbody/tr") #<== getting rows herefor row in rows:
        print (row.text) <== I am printing all row data, if you want cell data please update the logic accordingly
    time.sleep(randint(1, 5)) #<== this step is optional

Solution 2:

I believe you can read data directly using url instead of trying for pagination, this will lead to less sync issues because of which script might be failing

  1. Use this xpath to get total no of pages for the security-updates table. //*[@id="jumpPoint"]/div[3]/div/div/div[2]/div/div[2]/dir-pagination-controls/ul/li[11]

  2. Run loop till page count get from step 1 Inside loop pass page number in below url and send get request https://access.redhat.com/security/security-updates/#/security-advisories?q=&p=page_number&sort=portal_publication_date%20desc&rows=10&portal_advisory_type=Security%20Advisory&documentKind=PortalProduct

  3. wait for page to load

  4. Read data from table populated on page

  5. This process will run till the pagination count

  6. Incase you find specific error that site has blocked the user then you can refresh the page with same page_number.

Post a Comment for "Accessing The Next Page Using Selenium"