pythonselenium-webdriverwebdrivertraceback

Can't write a loop in Selenium Python


I can't write a loop in selenium python, I'm trying to make a bot that it'll write a descripton of products. The loop was worked successfully for the first product but when comes second product it gives a traceback error

productList = driver.find_element(By.XPATH, '//*[@id="product_form"]/table/tbody').find_elements(By.CLASS_NAME, "no-padding")

for index in range(len(productList)):
    productList = driver.find_element(By.ID, "product_form").find_elements(By.CLASS_NAME, "no-padding")                           
    productList[index].find_element(By.TAG_NAME, 'a').click()
    time.sleep(4)
    driver.switch_to.frame('content_ifr')
    driver.find_element(By.TAG_NAME, "p").send_keys("TEST")
    time.sleep(3)
    driver.back()
    time.sleep(5)
Traceback (most recent call last):
 File "c:\Users\deniz\OneDrive\Masaüstü\Python\muzayedebot.py", line 36, in <module>
 productList = driver.find_element(By.XPATH, '//*[@id="product_form"]/table/tbody').find_elements(By.CLASS_NAME, "no-padding")
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\deniz\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 741, in find_element
 return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\deniz\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
 self.error_handler.check_response(response)
File "C:\Users\deniz\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
 raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="product_form"]/table/tbody"}
   (Session info: chrome=126.0.6478.182); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception    
Stacktrace:
 GetHandleVerifier [0x00007FF7F1A7EEB2+31554]
         (No symbol) [0x00007FF7F19F7EE9]
         (No symbol) [0x00007FF7F18B872A]
         (No symbol) [0x00007FF7F1908434]
         (No symbol) [0x00007FF7F190853C]
         (No symbol) [0x00007FF7F194F6A7]
         (No symbol) [0x00007FF7F192D06F]
         (No symbol) [0x00007FF7F194C977]
         (No symbol) [0x00007FF7F192CDD3]
         (No symbol) [0x00007FF7F18FA33B]
         (No symbol) [0x00007FF7F18FAED1]
 GetHandleVerifier [0x00007FF7F1D88B2D+3217341]
 GetHandleVerifier [0x00007FF7F1DD5AF3+3532675]
 GetHandleVerifier [0x00007FF7F1DCB0F0+3489152]
 GetHandleVerifier [0x00007FF7F1B2E786+750614]
         (No symbol) [0x00007FF7F1A0376F]
         (No symbol) [0x00007FF7F19FEB24]
         (No symbol) [0x00007FF7F19FECB2]
         (No symbol) [0x00007FF7F19EE17F]
 BaseThreadInitThunk [0x00007FF824D0257D+29]
 RtlUserThreadStart [0x00007FF8254AAF28+40]

Solution

  • This is basic code to do loop on Selenium, go in element that change page, go back and continue iteration

    Take care using same variable name

    productList = driver.find_elements(...) #list
    
    for index in range(len(productList)):
    
        #Do your work stuff
        productList[index].click()
    
        #Go BACK
        driver.back()
    
    
        #Get list again at end but inside loop to dont get error element changed in DOOM
        #same line before start loop
        productList = driver.find_elements(...) #list