【发布时间】:2021-12-08 19:15:40
【问题描述】:
我正在尝试通过网络抓取此page,并且我正在寻找一种使用 selenium python 单击加载更多按钮的方法。我试过这些代码
driver.find_element(By.LINK_TEXT, "Load more").click()
driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[1]/main/div[2]/div[1]/div/button/span').click()
driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[1]/main/div[2]/div[1]/div/button').click()
但以上都没有工作的主要代码,我的替代解决方案是使用这样的滚动......
def infinite(driver):
scroll_pause_time = 10
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(scroll_pause_time)
try:
driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[1]/main/div[2]/div[1]/div/button').click()#This doesn't work
except:
print('No button')
new_height = driver.execute_script("return document.body.scrollHeight")
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
print('yeah!')
if new_height == last_height:
# If heights are the same it will exit the function
break
last_height = new_height
【问题讨论】:
标签: python selenium selenium-webdriver web-scraping beautifulsoup