简体   繁体   中英

click through list elements with selenium python

The given list contains the references which will paste into the xpath ids (please find it below in my code) where x are the indexes of the elements.

I want to go through on all elements and click one by one by referring with its indexes, 'like so'

m_list = ['message0', 'message1', 'message2', 'message3', 'message4']
for x in range(0, len(m_list)):
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
        (By.XPATH, f'//*[@id="{str(m_list[int(x)])}"]'))).click()
time.sleep(2)

This exception is common when you use Explicit wait which is WebDriverWait. This is expected since you wait for a fixed time for the element to be clickable. If the element was not found within that time, this exception is thrown. You might want to increase that time. The explicit wait can only be applied for specified elements, so if you are trying to click a paragraph, it won't work. If your elements appear after your initial click, that sleep command should also be in the loop, or you can use Implicit Wait.

Also if you want to iterate your list, you can use;

for i in m_list:
    WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.XPATH, f'//*[@id="{i}"]'))).click()

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