如何点击下一页

时间:2019-11-17 10:35:53

标签: python selenium-webdriver selenium-chromedriver

我试图单击下一页,页面的格式为1,2,3,4,5,6,7,8,9,10。我不知道首页。

第1页的xpath为'//*[@id="gvSearchDentistlist"]/tbody/tr[52]/td/table/tbody/tr/td[1]/a',第2页和第3页的依此类推。现在,我必须单击下一页直到结束。如何使用xpath单击?

from selenium import webdriver
from bs4 import BeautifulSoup as bs
import time

driver = webdriver.Chrome('C:\chromedriver.exe')
driver.get(url)
driver.maximize_window()

soup = bs(driver.page_source, 'html.parser')
table = soup.find('table',{'id':'gvSearchDentistlist'})

next_page = True
while next_page == True:
    soup = bs(driver.page_source, 'html.parser')
    table = soup.find('table',{'id':'gvSearchDentistlist'})
    try:
        rows = table.find_all('tr')
        for row in rows:
            if len(row.find_all('td')) == 6:
                data = row.find_all('td')
                name = data[1].text.strip()
                print("NAME:"+name)
                root_url = data[5].input['onclick'].split(",")[4]
                link ='url1'+root_url
                print("LINK:"+link)
    except:
        pass
    time.sleep(5)
try:
     tr = driver.find_element_by_xpath('//*[@id="gvSearchDentistlist"]/tbody/tr[52]/td/table/tbody/tr/td[1]').click()
except:
    print ('No more pages')
    next_page=False
driver.close()

0 个答案:

没有答案