简体   繁体   中英

get total pages number of webpage python

I read a lot on StackOverflow but couldn't find anything that was apt for my situation.

So, I am trying to open a website and search for a keyword. I want to go on every page and get the price. I will do this for all the pages.

Initially, I tried clicking on the next button but the 'Next button' on this website doesn't get disabled. For example, if there are 15 pages, so on the 15th page the next page button should get disabled, but I can click on it infinite times. After the 15th page, on clicking on it, it just shows the 15th page only.

So I am getting the totalpages count and using while loop like follows: Can anyone help me syntactically? I did the following:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

#open a website and search a keyword

from  selenium import webdriver
driver =webdriver.Firefox()

driver.get('https://www.insight.com/en_US/search.html')


list=['printer','printer cartridges','printer accessories']
for searchelement in list:
inputElement = driver.find_element_by_id("search-tab-banner-box-input")
inputElement.clear()
inputElement.send_keys(list)

wait( driver, 30 ).until( EC.visibility_of_element_located( (By.ID, "search-tab-banner-box-input") ) ).send_keys(Keys.ENTER )
inputElement.send_keys(Keys.TAB )

i=1
#Get total number of pages in search result and navigate through
 # I want to do something like this. this is my logic not real code
totalpages= enumerate(driver.gettotalnumber of pages)
while (i<totalpages)

            driver.find_element_by_class_name( "ion-chevron-right" ).click()
            i+=1
#Get total number of pages

How can I get the total number of pages in that variable?

price='0'  
while True:
     if price!=driver.find_element_by_class_name("prod-price"):
           driver.find_element_by_class_name( "ion-chevron-right" ).click()
     else:
           break
     i+=1
     price=driver.find_element_by_class_name("prod-price")

#Get total number of pages
print('the total number of pages is ', i)

This should work. It compares the price of the pages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM