简体   繁体   English

两个 for 循环 - 我如何运行

[英]two for loops - how do I run

I'm running this on Python (selenium):我在 Python (selenium) 上运行这个:

I can run this successfully for one iteration after that, it doesn't recognise the outer loop and shows an error.之后我可以成功运行一次迭代,它无法识别外部循环并显示错误。

elements = browser.find_elements_by_xpath('//*[@id="dtree0"]/div/a')
names=[]
for elem in elements:
    names.append(elem.text)
    print(names)

 for id in range (2, 170):
        for i in range(0, len(elements)):
            elements = browser.find_elements_by_xpath ('//*[@id="dtree0"]/div/a')
            elem = elements[i]
    #     #         # only click the elements in the names list (this level)
            if elem.text in names:
                try:
                    elem.click()
                except WebDriverException:
                     pass  # ignore if elem is not clickable
            # browser.find_elements_by_id("stree2").click()
            my_id = "stree{}".format(id)
            browser.find_element_by_id(my_id).click()
            browser.find_element_by_xpath (
                    '/html/body/center[2]/form/table[1]/tbody/tr/td[3]/table/tbody/tr[5]/td[1]/a[1]/img').click ()
            browser.find_element_by_xpath ('/html/body/center[2]/form/table[2]/tbody/tr/td[4]/input').click ()
            browser.find_element_by_xpath ('/html/body/center/form/table[2]/tbody/tr/td[5]/a').click ()
            sleep (5)
            browser.find_element_by_xpath ('//*[@id="personas"]/b').click ()
            browser.find_element_by_xpath('//*[@id="menu_personas"]/a[2]').click()

How can I modify this so that it runs both of these for loops on every iteration.我如何修改它以便它在每次迭代时都运行这两个 for 循环。

The error is as follows: selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable错误如下: selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

I believe this is because the "Stree" part is not being understood by the program since the outer for loop is not working.我相信这是因为程序无法理解“Stree”部分,因为外部 for 循环不起作用。

The webpage looks like this:该网页如下所示: 在此处输入图片说明

The folders part is the "elements" and the "id" part are the files.文件夹部分是“元素”,“id”部分是文件。 I want to open the folders and download the files all of them.我想打开文件夹并下载所有文件。

You get the length of elements and then change elements by assigning to it inside the loop.您获得元素的长度,然后通过在循环内分配给它来更改元素。 This is because of your double use of elements.这是因为您对元素的双重使用。 It should be 2 different variables.它应该是2个不同的变量。

for id in range (2, 170):
    for i in range(0, len(elements)):
        elements_2 = browser.find_elements_by_xpath ('//*[@id="dtree0"]/div/a')
        elem = elements_2[i]

That is if the second loop is inside the first loop.也就是说,如果第二个循环在第一个循环内。 Your indentation is unclear.你的缩进不清楚。

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

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