no such element: Unable to locate element using chromedriver and Selenium in production environment

后端 未结 4 551
故里飘歌
故里飘歌 2021-01-04 13:29

I have a problem with selenium chromedriver which I cannot figure out what\'s causing it. Some weeks ago everything was working OK, and suddenly this error started to show u

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 13:44

    Whenever I encounter strange issues in Selenium like this, I prefer retrying to find the particular element which is causing intermittent troubles. One way is to wrap it around a try-except block:

    try:
       sidebar = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
    except NoSuchElementException:
       time.sleep(10)
       print("Unable to find element in first time, trying it again")
       sidebar = browser.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
    

    You could also put the try code in a loop with a suitable count variable to make the automation code work. (Check this). In my experience with JAVA, this idea has resolved multiple issues.

提交回复
热议问题