Search code examples
pythonseleniumcycle

(Python, Selenium WD) Нow to receive and transfer the list to a cycle?


I have this code

lst = ["Appearence","Logotype", "Catalog", "Product Groups", "Option Groups","Manufacturers","Suppliers",
   "Delivery Statuses","Sold Out Statuses", "Quantity Units", "CSV Import/Export", "Countries","Currencies","Customers"]

for item in lst:
    wd.find_element_by_link_text(item).click()
    assert wd.title != None

I not want to write list by hand. I want to receive the list - lst directly from the browser. I use

m = wd.find_elements_by_css_selector('li[id=app-]')
print(m[0].text)

Appearence

I don't know how to transfer the list to a cycle

look this picture screen browser

Please help me to understand how to use the list and to transfer it to a cycle


Solution

  • In your example variable m will be a list of WebElements you get the length of it and iterate CSS pseudo selector :nth-child() with a range:

    m = wd.get_elements_by_css_selector('li#app-')
    
    for elem in range(1, len(m)+1):
        wd.get_element_by_css_selector('li#app-:nth-child({})'.format(elem)).click()
        assert wd.title is not None
    

    In the for loop it will iterate over a range of integers starting with 1 and ending with the length of the element list (+1 because is not inclusive), the we will click the nth-child of the selector using the iterating number, .format(elem) will replace th {} appearance in the string for the elem variable, in this case the integer iteration.