0

I am writing a code to download some files from a webpage. The code starts fine and there are 27 files to download, but after the first 5 downloads, I get a ElementClickInterceptedException error for the following elements. Can anyone tell me why the code stops downloading the rest of the files?

Here is the (part of the) code:

        actions = ActionChains(driver)

        xlbr2 = driver.find_elements_by_xpath("//*[@class='mf mf-xbrl']")
        print(len(xlbr2))
        for link in range(4, 27):
            time.sleep(2)
            print(link)
            try:
                xlbr2 = driver.find_elements_by_xpath("//*[@class='mf mf-xbrl']")
                xlbr2 = driver.find_elements_by_xpath("//*[@class='mf mf-xbrl']")[link]
                actions.move_to_element(xlbr2).perform()
                xlbr2.click()
                # xlbr2 = driver.find_elements_by_xpath("//*[@class='mf mf-xbrl']")[link].click()
                time.sleep(1)
                download = driver.find_element_by_xpath('/html/body/div[2]/div[8]/div/div/div/div[1]/div/div[2]/ul/div/div[4]/a/button/i')
                print('downloading file...')
                download.click()
                time.sleep(2)
                driver.back()
                # time.sleep(2)

            except Exception as err:
                print(f"{type(err).__name__} was raised: {err}")
Bakaveli
  • 1
  • 2

2 Answers2

0

You did not share a link to the page you are working on, so I can't know what is the actual problem there, however I can guess you have to scroll the page in order to reach those elements.
It so something like this will help:

actions = ActionChains(driver)
xlbr2 = driver.find_elements_by_xpath("//*[@class='mf mf-xbrl']")
print(len(xlbr2))
for link in range(len(xlbr2)):
    time.sleep(2)
    print(link)
    try:
        xlbr2 = driver.find_elements_by_xpath("//*[@class='mf mf-xbrl']")[link]  
        actions.move_to_element(xlbr2).perform()            
        xlbr2.click()
        time.sleep(1)
        download = driver.find_element_by_css_selector('button.saveButton')
        print('downloading file...')
        download.click()
        time.sleep(2)
        driver.back()
        time.sleep(2)
    except Exception as err:
        print(f"{type(err).__name__} was raised: {err}")

You will have to import this

from selenium.webdriver.common.action_chains import ActionChains

It is also possible that there is some accept cookie banner on the bottom of the page that you should close...
need to see the actual page to give more precise answer.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • thanks, this is the page: 'https://maya.tase.co.il/reports/finance', I want to download all the XLBR files. The problem is not clicking the download button when on the XLBR download page, the problem is that after the first 5 downloads, the script doesn't go on the download page, while there is clearly a XLBR download page to go. As it does for the first 5 links. In other words.. here is the problem -->xlbr2 = driver.find_elements_by_xpath("//*[@class='mf mf-xbrl']")[link].click() – Bakaveli Jan 13 '22 at 07:46
  • I have lines in the code to close the cookie banner at the start – Bakaveli Jan 13 '22 at 07:46
  • What do you see printed here: `print(len(xlbr2))`? 5 or 27 ? – Prophet Jan 13 '22 at 07:57
  • Also, please try adding a sleep `time.sleep(2)` after `driver.back()`. – Prophet Jan 13 '22 at 08:04
  • Please let me know the if additional delay helped? – Prophet Jan 13 '22 at 09:41
  • it prints 27. the extra delay does not work. – Bakaveli Jan 13 '22 at 10:21
  • if I let the code start from range 5 instead of 0, I directly get the error... – Bakaveli Jan 13 '22 at 10:21
  • What error? That's interesting – Prophet Jan 13 '22 at 10:22
  • the same: ElementClickInterceptedException was raised: Message: element click intercepted – Bakaveli Jan 13 '22 at 12:16
  • What code line is giving this error? – Prophet Jan 13 '22 at 12:44
  • xlbr2 = driver.find_elements_by_xpath("//*[@class='mf mf-xbrl']")[link].click(), its the same original error which I based this question on – Bakaveli Jan 13 '22 at 13:30
  • Looks like it's my fault.. We need to scroll `xlbr2` element. Please let me know if now it works – Prophet Jan 13 '22 at 13:36
  • I used this line to scroll XLBR2: driver.execute_script("arguments[0].scrollIntoView(true);", xlbr2[link]) but still get the same ElementClickInterceptedException error! – Bakaveli Jan 13 '22 at 16:02
  • it's not about scrolling, somehow it is only possible to click on the first five elements while there are 27 to click... – Bakaveli Jan 13 '22 at 17:56
  • I keep getting this error: StaleElementReferenceException was raised: Message: stale element reference: element is not attached to the page document. This is the code now: – Bakaveli Jan 13 '22 at 18:02
  • I adjusted the code to the current version – Bakaveli Jan 13 '22 at 18:03
  • What line gives `StaleElementReferenceException`? – Prophet Jan 13 '22 at 18:24
  • this one: actions.move_to_element(xlbr2).perform() – Bakaveli Jan 13 '22 at 19:33
0

driver.execute_script('arguments[0].click()', element) This solved the issue

Bakaveli
  • 1
  • 2