[英]Interacting with JavaScript object when web scraping with python and selenium
我正在嘗試加載更多評論,這是通過單擊JavaScript對象加載的。 然后抓取頁面。 為了驗證這一點,我在單擊“加載更多”按鈕之前和之后打印頁面包含的評論數(在ap標簽內)。 但是,它在前后指向相同數量的p標簽。 如果您查看該頁面,它將有更多評論。 我要去哪里錯了?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.PhantomJS(executable_path='PATH_TO.../phantomjs')
driver.get('http://www.ratemyprofessors.com/ShowRatings.jsp?tid=1500075')
comments = driver.find_elements_by_tag_name('p')
print('Before', len(comments))
time.sleep(1)
try:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'loadMore')))
time.sleep(1)
finally:
comments = driver.find_elements_by_tag_name('p')
print('After', len(comments))
driver.close()
請注意,我嘗試了'loadMore'和'loadmoreBlog'..均無效。 提前非常感謝您的回答。
我需要添加
element.click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.