1

I have this code to download several files from URL. It works partially because only downloads the first files that are visible in page. The issue it seems to be that when each file is downloaded the page is not scrolling down automatically.

I receive this error:

Element <a abc-id="0" href="#" class="todown" nc="0">Download</a> 
is not clickable at point (1474, 639). Other element would receive 
the click: <div id="footer">...</div>

How would be to add an automatic scroll down in my script below?

from time import sleep
from selenium import webdriver

driver = webdriver.Chrome("C:\webdrivers\chromedriver.exe")

driver.get ("http://www.examplesite.com/")
time.sleep(3)

tr = driver.find_elements_by_xpath("//*[@id='dwn']/div/table[1]/tbody[1]/tr") 

for i in range(1,len(tr)):
    driver.find_element_by_xpath("//*[@id='dwn']/div/table[1]/tbody[1]/tr["+str(i)+"]/td[3]/a").click()

Thanks for any help.

Ger Cas
  • 2,188
  • 2
  • 18
  • 45
  • 1
    you can try this: `driver.execute_script('window.scrollBy(0, 500)')` . It will scroll down 500 pixels along the y-axis – Artemij Rodionov May 11 '19 at 23:35
  • @Artemiy Hi Artemiy. Thanks for you answer. Adding what you suggest will scroll down 500 pixels for each download? The page is kind of large since are more than 500 files to bottom. – Ger Cas May 12 '19 at 00:00
  • @Artemiy Thanks. I tried your suggestion and the scroll down it works but I needed to test several different values for Y in order to make it work. 500 too much, still 50 too much and script stops after a few downloads because scroll were faster than the downloads. Y = 10 or 30 not enough and script stops after a few downloads because the downloads were faster than scroll. With Y=40 it works up to the end. I ask if there is an automatic way to set this or only setting a fixed Y value? – Ger Cas May 12 '19 at 09:08

1 Answers1

0

I would just add location_once_scrolled_into_view so that corresponding row will be scrolled into view.

for i in range(1,length_of_tr):
    ele = driver.find_element_by_xpath("//*[@id='dwn']/div/table[1]/tbody[1]/tr["+str(i)+"]/td[3]/a")
    ele.location_once_scrolled_into_view
    ele.click()
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Thanks for answer. I tested your code but I´m not sure why the scroll doesn´t work with this method. I tested what `Artemiy` and in that way the scroll down it works but I needed to test sevelar different values for Y in order to work. 500 too much, 50 too much and script stops after a few downloads, 10, 30 not enough and script stops after a few downloads. With Y=40 it works up to the end. – Ger Cas May 12 '19 at 09:03