简体   繁体   中英

two for loops - how do I run

I'm running this on 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.

The error is as follows: 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.

The webpage looks like this: 在此处输入图片说明

The folders part is the "elements" and the "id" part are the files. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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