簡體   English   中英

如何單擊按鈕並使用python-selenium獲取新頁面的來源?

[英]How to click the button and get the source of new page with python-selenium?

我希望單擊next按鈕直到最后,然后抓取頁面的所有來源。 我的代碼如下:

from selenium import webdriver  
from selenium.common import exceptions  
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox()  
browser.get('https://s.taobao.com/search?q=%E5%8F%A3%E7%BD%A9+%E9%9D%A2%E7%BA%B1')  
sources = []
for i in range(100):
    sources.append(browser.page_source)
    browser.find_element_by_css_selector("li.item.next > a.J_Ajax.num.icon-tag").click()
time.sleep(8)
browser.quit()

但是所有來源均來自第一頁,當我已經在每頁中等待8秒鍾並且看到圖像已完成加載時。 您能告訴我原因是什么,如何解決? 謝謝!

我相信頁面上顯示的搜索結果是AJAX,因此您將無法在HTML源代碼中看到它們,因為AJAX是動態的並且僅存儲在內存中。

您可以通過在DOM中查找元素來訪問數據,例如使用CSS定位器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM