Robertson123
Robertson123

Reputation: 17

Webdriver&Python: Iterating through the list and adding a new element

I have a problem with my test - adding a new query on a site. There are 2 existing queries already with id-0, id-1... You don't know how many queries will be added so I want to add a new query without giving the specific id number. I was trying to start with iterating but I don't know how to proceed with this code...

list_of_elements = wd.find_elements_by_css_selector("cssselector")
    for element in list_of_elements:
        wd.find_element_by_link_text("Add test").click()
        queries = wd.find_elements_by_css_selector('[id*="id_query-1-title"]')
        index = str(len(queries))
        wd.find_element_by_id('id_query-' + index + '-title').click()
        wd.find_element_by_id('id_query-' + index + '-title').clear()
        wd.find_element_by_id('id_query-' + index + '-title').send_keys("ABC")

Upvotes: 0

Views: 129

Answers (1)

Robertson123
Robertson123

Reputation: 17

OK, I have a solution. Thank you for your help!

test = wd.find_elements_by_class_name('classname')
    test_count = len(test)
    test_string = str(test_count - 1)
    wd.find_element_by_id('id_query-' + test_string + '-title').click()
    wd.find_element_by_id('id_query-' + test_string + '-title').send_keys("ABC")

Upvotes: 1

Related Questions