无法在 Selenium Python 中编写循环

问题描述 投票:0回答:1

我无法在 selenium python 中编写循环,我正在尝试制作一个机器人来编写产品描述。对于第一个产品,循环工作成功,但是当出现第二个产品时,它会出现回溯错误

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]
python selenium-webdriver webdriver traceback
1个回答
0
投票

这是在 Selenium 上进行循环的基本代码,进入更改页面的元素,返回并继续迭代

小心使用相同的变量名

productList = find_elements(...) #list

for index in range(len(productList)):

    #Do your work stuff
    .click()

    #Go BACK
    .back()


    #Get list again at end but inside loop to dont get error element changed in DOOM, same line befor start loop
    productList = find_elements(...) #list
© www.soinside.com 2019 - 2024. All rights reserved.