提问人:Waqas Ahmed 提问时间:8/7/2023 最后编辑:Ajeet VermaWaqas Ahmed 更新时间:8/7/2023 访问量:26
WebDriver.Wait() 在用于无限页面滚动的 While 循环中不起作用
WebDriver.Wait() not working inside a While loop used for infinite page scroll
问:
我想在 while 循环中使用函数。While loop 用于滚动无限滚动网站。我想在last_scroll和new_scroll之间等待一下,以加载页面内容。静态工作正常,但我不想使用它。WebDriver.Wait()
time.sleep(10)
try:
driver.get(url)
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'pb-xl')))
self.writeToFile('test.txt','Page is loaded\n')
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
print("Scrolling down...")
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
print("Wait for new content to load...")
# Wait to load page
WebDriverWait(driver, 100).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'pb-xl')))
# time.sleep(10)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
print("Reached the end of the page.")
last_height = new_height
print("Loading next page")
# print(driver.page_source)
self.writeToFile('page_source.txt',driver.page_source)
except TimeoutException:
self.writeToFile('test.txt','Timed out waiting for the element to appear\n')
except Exception as e:
self.writeToFile('test.txt',f'Error: {str(e)}\n')
答: 暂无答案
评论