python selenium查找所有elementS StaleElementReferenceException

时间:2019-03-16 13:12:34

标签: python selenium

EDIT2:对于将来有相同问题的人们:问题是,一旦我为for循环声明了all_planets变量,无论我在for循环中将其更改为什么,它都不会更新。所以我用了while循环而不是for循环:

 all_planets = c.driver.find_elements_by_xpath('//*[contains(@class, \'smallplanet\')]')
                element_clicking = 0

                while element_clicking < len(all_planets):
                        all_planets = c.driver.find_elements_by_xpath('//*[contains(@class, \'smallplanet\')]')

                        all_planets[element_clicking].click()
                        element_clicking += 1

                        try:
                            if metalMineLVL() in range(10,15,1):
                                if roboFabLVL() <(5):
                                    print('Building Robo')
                                    c.roboFab.build()
                                    continue
                                elif forschLabLVL() <(5):
                                    print('Building Research center')
                                    c.forschLab.build()
                                    continue
                                elif schWerftLVL() <(5):
                                    print('Building ship bay')
                                    c.schWerft.build()
                                    continue
                                else:
                                    cycles.buildMinesCycle()

                            elif metalMineLVL() in range(15, 20, 1):
                                if roboFabLVL() <(8):
                                    print('Building Robo')
                                    c.roboFab.build()
                                    continue
                                elif forschLabLVL() <(8):
                                    print('Building Research center')
                                    c.forschLab.build()
                                    continue
                                elif schWerftLVL() <(8):
                                    print('Building ship bay')
                                    c.schWerft.build()
                                    continue
                                elif astroTechLVL() < 3:
                                    print('Researching astrological technology')
                                    c.astroTech.build()
                                elif btnnumKoloLVL() <(1):
                                    print('Building colony ship')
                                    c.btnnumKolo.build(1)
                                    continue
                                elif spioTechLVL() <(3):
                                    print('Researching spy technology')
                                    c.spioTech.build()
                                    continue
                                elif btnnumGTLVL() <(3):
                                    print('Building greater tansport ship')
                                    c.btnnumGT.build(1)
                                    continue
                                elif compTechLVL() <(3):
                                    print('Building researching computing science')
                                    c.compTech.build()

                                else:
                                    cycles.buildMinesCycle()
                            elif metalMineLVL() >= (20):
                                if roboFabLVL() <(10):
                                    print('Building robo')
                                    c.roboFab.build()
                                    continue
                                elif forschLabLVL() <(10):
                                    print('Building research center')
                                    c.forschLab.build()
                                    continue
                                elif schWerftLVL() <(10):
                                    print('Building ship bay')
                                    c.schWerft.build()
                                    continue
                                elif btnnumKoloLVL() <(1):
                                    print('Building colony ship')
                                    c.btnnumKolo.build(1)
                                    continue
                                elif spioTechLVL() <(8):
                                    print('Researching spy tehcnology')
                                    c.spioTech.build()
                                    continue
                                elif btnnumGTLVL() <(10):
                                    print('Building greater transport ship')
                                    c.btnnumGT.build(1)
                                    continue
                                else:
                                    cycles.buildMinesCycle()
                            else:

                                cycles.buildMinesCycle()




                        except Exception:
                            traceback.print_exc()
                            return

所以我有这段代码,给了我StaleElementReferenceException。我确实知道为什么得到它(首先我搜索具有特定类的所有元素,然后尝试在for循环中循环访问它们,这给了我错误),但是我不知道如何解决问题。这里的代码:

all_planets = c.driver.find_elements_by_xpath('//*[contains(@class, \'smallplanet\')]')

for element in all_planets:
  element.click()
  //click on some things etc

问题是,一旦程序尝试访问第二个元素,就会引发StaleElement错误。我试图以某种方式重新加载Element,但是我不知道它是如何工作的,例如:

all_planets = c.driver.find_elements_by_xpath('//*[contains(@class, \'smallplanet\')]')

for element in all_planets:
  try:
     element.click()
     //click on some things etc
  except:
     c.driver.find_elements_by_xpath('//*[contains(@class, \'smallplanet\')]')
     element.click()

但是这也不起作用。有没有一种方法可以选择特定的WebElement,然后重新加载该元素?例如:

获取所有Web元素,如果出现staleError,只需重新加载要访问的特定WebElement?

编辑: Okey,所以我试了一下,以某种方式不起作用。我还使用调试器查看了python是否确实执行了该命令,并且看起来确实如此。

这里是实际代码:

all_planets = c.driver.find_elements_by_xpath('//*[contains(@class, \'smallplanet\')]')
                for element in all_planets:
                    
                        element.click()

                        try:
                            if metalMineLVL() in range(10,15,1):
                                if roboFabLVL() <(5):
                                    print('Building Robo')
                                    c.roboFab.build()
                                    continue
                                elif forschLabLVL() <(5):
                                    print('Building Research center')
                                    c.forschLab.build()
                                    continue
                                elif schWerftLVL() <(5):
                                    print('Building ship bay')
                                    c.schWerft.build()
                                    continue
                                else:
                                    cycles.buildMinesCycle()

                            elif metalMineLVL() in range(15, 20, 1):
                                if roboFabLVL() <(8):
                                    print('Building Robo')
                                    c.roboFab.build()
                                    continue
                                elif forschLabLVL() <(8):
                                    print('Building Research center')
                                    c.forschLab.build()
                                    continue
                                elif schWerftLVL() <(8):
                                    print('Building ship bay')
                                    c.schWerft.build()
                                    continue
                                elif astroTechLVL() < 3:
                                    print('Researching astrological technology')
                                    c.astroTech.build()
                                elif btnnumKoloLVL() <(1):
                                    print('Building colony ship')
                                    c.btnnumKolo.build(1)
                                    continue
                                elif spioTechLVL() <(3):
                                    print('Researching spy technology')
                                    c.spioTech.build()
                                    continue
                                elif btnnumGTLVL() <(3):
                                    print('Building greater tansport ship')
                                    c.btnnumGT.build(1)
                                    continue
                                elif compTechLVL() <(3):
                                    print('Building researching computing science')
                                    c.compTech.build()

                                else:
                                    cycles.buildMinesCycle()
                            elif metalMineLVL() >= (20):
                                if roboFabLVL() <(10):
                                    print('Building robo')
                                    c.roboFab.build()
                                    continue
                                elif forschLabLVL() <(10):
                                    print('Building research center')
                                    c.forschLab.build()
                                    continue
                                elif schWerftLVL() <(10):
                                    print('Building ship bay')
                                    c.schWerft.build()
                                    continue
                                elif btnnumKoloLVL() <(1):
                                    print('Building colony ship')
                                    c.btnnumKolo.build(1)
                                    continue
                                elif spioTechLVL() <(8):
                                    print('Researching spy tehcnology')
                                    c.spioTech.build()
                                    continue
                                elif btnnumGTLVL() <(10):
                                    print('Building greater transport ship')
                                    c.btnnumGT.build(1)
                                    continue
                                else:
                                    cycles.buildMinesCycle()
                            else:

                                cycles.buildMinesCycle()




                        except Exception:
                            traceback.print_exc()
                            return

                        finally:

                            all_planets = c.driver.find_elements_by_xpath('//*[contains(@class, \'smallplanet\')]')
                            c.time.sleep(1)

错误:

Traceback (most recent call last):
  File "C:\Users\leosc\PycharmProjects\ogameBot\guideline.py", line 27, in guideline
    element.click()
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=72.0.3626.121)
  (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17134 x86_64)

0 个答案:

没有答案