You should consider doing something like
xpBanners = '//div[contains(@id,"dismissible")]/ytd-thumbnail/a'
## WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, xpBanners)))
## scroll to load all Banners (will explain below)
Banners_imgs = driver.find_elements_by_xpath(f'{xpBanners}//img')
Banners_src = [b.get_attribute('src') for b in Banners_imgs]
instead of
Banners_src = []
for element in Banners:
Banners_src.append(element.find_element_by_xpath('.//img').get_attribute('src'))
because element.find_element...
behaves unexpectedly sometimes [see this and this for example].
On my browser, I start with just 30 //div[contains(@id,"dismissible")]/ytd-thumbnail/a
and have to scroll to the bottom to load the next 30. Normally, I'd suggest that you either scroll to the last element using
# import time
scrollCt = 10 # 5 should be enough, but just in case...
scrollWait = 2 # in seconds - adjust according to loading speed
xpBanner = '//div[contains(@id,"dismissible")]/ytd-thumbnail/a'
## wait to load... as necessary
for x in range(scrollCt):
lastBanner = driver.find_elements_by_xpath(xpBanner)[-1]
driver.execute_script('arguments[0].scrollIntoView(false);', lastBanner)
time.sleep(scrollWait)
or scroll to the bottom of the page with
for x in range(scrollCt):
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
time.sleep(scrollWait)
But neither of those seem to work with YouTube pages... So instead, you could try to scrolling bit by bit with PGDN
.
# import time
# from selenium.webdriver.common.keys import Keys
pgCt = 10 # adjust according to your browser (mine needs 7 - used 10 just in case)
scrollCt = 10 # 5 should be enough, but just in case...
scrollWait = 2 # in seconds - adjust according to loading speed
for x in range(scrollCt):
driverD.find_element(By.XPATH, '//body').send_keys(Keys.PAGE_DOWN)
if x%pgCt == 0: time.sleep(scrollWait)