简体   繁体   English

Scraper不会停止点击下一页按钮

[英]Scraper doesn't stop clicking on the next page button

I've written a script in python in combination with selenium to get some names and corresponding addresses displayed upon a search and the search keyword is "Saskatoon". 我在python中编写了一个与selenium结合使用的脚本,以便在搜索时显示一些名称和相应的地址,搜索关键字是“Saskatoon”。 However, the data, in this case, traverse multiple pages. 但是,在这种情况下,数据遍历多个页面。 My script almost does everything except for one thing. 除了一件事,我的剧本几乎可以做所有事情。

  1. It still runs even though there are no more pages to traverse. 即使没有更多页面可以遍历,它仍然会运行。 The last page also holds ">" sign for next page option and is not grayed out. 最后一页还为下一页选项保留“>”符号,并且不会显示为灰色。

Here is the link: Page_link 这是链接: Page_link

Search_keyword: Saskatoon (in the city/town field). Search_keyword: Saskatoon (在城市/城镇地区)。

Here is what I've written: 这是我写的:

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("above_link")
time.sleep(3)

search_input = driver.find_element_by_id("cityField")
search_input.clear()
search_input.send_keys("Saskatoon")
search_input.send_keys(Keys.ENTER)

while True:
    try:
        wait.until(EC.visibility_of_element_located((By.LINK_TEXT, "›"))).click()
        time.sleep(2)
    except:
        break
driver.quit()

BTW, I've just taken out the name and address part form this script which I suppose is not relevant here. 顺便说一句,我刚刚从这个脚本中取出了名称和地址部分,我想这里没有相关内容。 Thanks. 谢谢。

您可以使用>按钮的class属性,如在最后一页上"ng-scope disabled" "ng-scope" - 在其余页面上 - "ng-scope"

wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@class='ng-scope']/a[.='›']"))).click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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