[英]Python Selenium - Clicking through pages without next button
我想通過單擊從多個網頁中檢索信息(參見圖1和2)。 問題是a)沒有下一個按鈕,並且b)即使頁面鏈接包含一個用於計數的數字,它也不會對手動更改編號做出反應(即不加載下一頁)。 這使任務變得棘手。
誰能幫忙解決這個問題?
鏈接的結構如下所示(無功能頁面) https://sample.io/address/ID#pageSize=100
更新:在Robbie W的幫助下可以正常工作。我正在使用的代碼是:
options.add_argument('windows-size = 1200 x 800')
browser = webdriver.Chrome(chrome_options = options)
browser.get('URL')
page_soup_1 = soup(browser.page_source, "lxml")
items_1 = page_soup_1.find_all("li", {"class": "page-item" })
LenofPage = pd.DataFrame()
count = pd.DataFrame()
for item in items_1 :
string = str(item)
Num = string[string.find('page-item')+23:string.find('\/li')-8]
LenofPage = LenofPage.append({'LenofPage': Num}, ignore_index = True)
Max_pagenum = LenofPage.max()
Max_pagenum_1 = int(Max_pagenum)
count = 1
#items_1 = page_soup.find_all("li", {"class": "page-item active"
}).next_sibling
while count < Max_pagenum_1:
link = browser.find_element_by_xpath('//li[contains(@class, "page-item")
and contains(@class,"active")]/following-sibling::li/a')
link.click()
count = count + 1
time.sleep(3)
print(count)
到達最后幾頁時,可能需要稍作修改,但是我建議使用XPath在當前選定的li
旁邊找到li
,然后單擊其中的a
標記。
//li[contains(@class, "page-item") and contains(@class,"active")]/following-sibling::li/a
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.