简体   繁体   English

Selenium python 在前五个元素之后停止迭代,而列表中有更多元素

[英]Selenium python stops iteration after the first five elements while there are more elements in the list

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.代码开始正常,有 27 个文件要下载,但在前 5 次下载后,我收到以下元素的 ElementClickInterceptedException 错误。 Can anyone tell me why the code stops downloading the rest of the files?谁能告诉我为什么代码停止下载文件的 rest?

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}")

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].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...')
        actions.move_to_element(download).perform()
        download.click()
        time.sleep(2)
        driver.back()

    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...也可能在页面底部有一些接受 cookie 横幅,您应该关闭...
need to see the actual page to give more precise answer.需要查看实际页面才能给出更准确的答案。

driver.execute_script('arguments[0].click()', element) This solved the issue driver.execute_script('arguments[0].click()', element) 这解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM