cant find the end of page by selenium
мне нужно прокрутить страницу видео на ютубе до конца. не могу определить конец страницы т.е когда прокручивание должен остановиться. I need to scroll page to the end,but i cant just defiend the end of the page!
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.keys import Keys
url = 'https://www.youtube.com/channel/UCEjpJz-JIzzaO0KMF8HgAew/videos'
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36",
'x-youtube-client-name': '1',
'x-youtube-client-version': '2.20200429.03.00',
}
def get_source_html():
driver = webdriver.Firefox(executable_path = 'C:\youtube_parser\geckodriver.exe')
driver.get(url=url)
driver.maximize_window()
try:
time.sleep(3)
i = 1
while True:
i += 1
# Use send_keys(Keys.HOME) to scroll up to the top of pag
if driver.find_element_by_tag_name(''):#here should be if for stop scroling
with open("source-page.html","w",encoding='utf-8') as file:
file.write(driver.page_source)
break
else:
driver.find_element_by_tag_name('body').send_keys(Keys.END)#this code is scroling
print('still scroling',i)
except Exception as ex:
print(ex)
finally:
driver.close()
driver.quit()
def main():
get_source_html()
if __name__ == "__main__":
main()
Ответы (1 шт):
Автор решения: Aleksey Kuchkin
→ Ссылка
Т.к. контент динамический, нельзя сразу получить всю высоту страницы, так:
h = driver.execute_script('return document.body.scrollHeight;')
А на ютубе можно на высоту окна смещаться
h_prev = 0
while True:
h = driver.execute_script('return window.innerHeight + window.scrollY;')
driver.execute_script(f'window.scrollTo(0,{h});')
time.sleep(1)
if h == h_prev:
break
h_prev = h