0

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)
S.B
  • 13,077
  • 10
  • 22
  • 49
  • 1
    OK, so what is the problem here? I see no indentation, what else? – Prophet Jan 13 '22 at 10:19
  • Well the code that I embedded into this topic starts to run, but raises the following error: Traceback (most recent call last): File "C:.../test.py", line 76, inWebDriverWait(driver,10).until(EC.element_to_be_clickable(File"C:...\Python\Python38-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen,stacktrace)selenium.common.exceptions.TimeoutException. In practice, it chooses the first element from the list on the webpage, but after that it crashes. Therefore my question could be how would I solve this issue? – usermate17921900 Jan 13 '22 at 10:56
  • can you share a link to that page? – Prophet Jan 13 '22 at 11:17

1 Answers1

0

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()
Ozgur O.
  • 97
  • 9
  • Thank you for your response, I guess I figured out that the problem is, it is a drop-down list and I have to click the main button all the time before I click the elements of the list. Do you have any further idea to solve this problem? If you have not, never mind, I'm already happy with your answer. :) – usermate17921900 Jan 13 '22 at 12:10
  • Glad I helped, if you are selecting, you should use the Select class. More here: https://stackoverflow.com/questions/7867537/how-to-select-a-drop-down-menu-value-with-selenium-using-python – Ozgur O. Jan 13 '22 at 12:35