for循环中的过时元素,即使我在每个循环开始时都在搜索元素

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

我正在编写一个for循环,以遍历行表并对每一行执行一个操作,然后保存。保存后,将自动重新加载网页。为了避免过时的元素异常,我在刷新网页后在循环的开头调用web元素。它在第一个循环中工作正常,但是在第二个循环中,它给了我一个过时的元素异常。行的位置不变。在第一个循环之后,这些元素清晰可见。

我不明白为什么,如果我每次在循环开始时都在搜索Web元素,而不是尝试通过存储的列表或变量进行访问。

有人对绕过这个有任何建议吗?还是我应该以不同的方式编写代码?

 counter = 0


for i in range(5):

  time.sleep(5)

   yp = WebDriverWait(driver, 10).until(
   EC.presence_of_all_elements_located((By.XPATH, "//table/tbody/tr/td[6]")))[counter]

  actionChains.double_click(yp).perform()
  x = driver.find_elements_by_xpath("//table/tbody/tr/td[6]")[counter].text
  new_leg_name = df.loc[df.loc[:,0] == x,1].values
  time.sleep(3)
  long_name_field = driver.find_element_by_name("id8")
  long_name_field.click()
  time.sleep(2)

  long_name_field.send_keys(Keys.CONTROL, 'a')
  long_name_field.send_keys(Keys.BACKSPACE)
  time.sleep(3)

  long_name_field.send_keys(new_leg_name)
  time.sleep(3)

  # add note to the bottom
  note = driver.find_element_by_name("id337")
  note.click()
  time.sleep(2)
  note.send_keys("entity name cleanup for data migration")
  time.sleep(2)


  #click on the save button
  save = driver.find_element_by_xpath("//span[contains(text(),'Save')]")
  save.click()
  time.sleep(6)

  counter += 1

  #click on the reload button again 

  reload =driver.find_element_by_xpath("/html/body/div[4]/div[2]/div[2]/div[1]/div[1]/div/div/div/div[6]/div/div/a[5]/span/span/span[1]")
  reload.click()
  time.sleep(5)
  print(counter)

陈旧元素错误

---------------------------------------------------------------------------
StaleElementReferenceException            Traceback (most recent call last)
<ipython-input-32-52f0e8daec6f> in <module>
     17     EC.presence_of_all_elements_located((By.XPATH, "//table/tbody/tr/td[6]")))[counter]
     18 
---> 19     actionChains.double_click(yp).perform()
     20     x = driver.find_elements_by_xpath("//table/tbody/tr/td[6]")[counter].text
     21     new_leg_name = df.loc[df.loc[:,0] == x,1].values

c:\users\o34639\appdata\local\programs\python\python37-32\lib\site-packages\selenium\webdriver\common\action_chains.py in perform(self)
     78         """
     79         if self._driver.w3c:
---> 80             self.w3c_actions.perform()
     81         else:
     82             for action in self._actions:

c:\users\o34639\appdata\local\programs\python\python37-32\lib\site-packages\selenium\webdriver\common\actions\action_builder.py in perform(self)
     74             if encoded['actions']:
     75                 enc["actions"].append(encoded)
---> 76         self.driver.execute(Command.W3C_ACTIONS, enc)
     77 
     78     def clear_actions(self):

c:\users\o34639\appdata\local\programs\python\python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

c:\users\o34639\appdata\local\programs\python\python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=78.0.3904.87)

我也尝试使用以下内容,但仍然收到陈旧的元素异常。

actionChains.double_click(driver.find_elements_by_xpath("//table/tbody/tr/td[6]")[counter].perform()

下面的html代码

<div class="x-grid-item-container" role="presentation" style="width: 4673px; transform: translate3d(0px, 0px, 0px);">
    <table id="revalgridview-3995-record-2238" role="presentation" data-boundview="revalgridview-3995" data-recordid="2238" data-recordindex="0" class="x-grid-item x-grid-item-selected" cellpadding="0" cellspacing="0" style=";width: 0px;padding-left: 0px;">
        <tbody>
            <tr class="  x-grid-row" role="row" aria-selected="true">
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-3996 x-grid-cell-first" role="gridcell" style="width:71px;" tabindex="-1" data-columnid="gridcolumn-3996">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">Yes</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-3997" role="gridcell" style="width:92px;" tabindex="-1" data-columnid="gridcolumn-3997">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">0101</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-3998" role="gridcell" style="width:85px;" tabindex="-1" data-columnid="gridcolumn-3998">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">0101</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-3999" role="gridcell" style="width:253px;" tabindex="-1" data-columnid="gridcolumn-3999">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">test entity</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4000" role="gridcell" style="width:134px;" tabindex="-1" data-columnid="gridcolumn-4000">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">USD</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4001" role="gridcell" style="width:253px;" tabindex="-1" data-columnid="gridcolumn-4001">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">0101</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4002" role="gridcell" style="width:85px;" tabindex="-1" data-columnid="gridcolumn-4002">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">BP0101</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4003" role="gridcell" style="width:358px;" tabindex="-1" data-columnid="gridcolumn-4003">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">T0101</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4004" role="gridcell" style="width:197px;" tabindex="-1" data-columnid="gridcolumn-4004">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">test entity</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4005" role="gridcell" style="width:134px;" tabindex="-1" data-columnid="gridcolumn-4005">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">UNITED STATES OF AMERICA</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4006" role="gridcell" style="width:71px;" tabindex="-1" data-columnid="gridcolumn-4006">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">USNYC</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4007" role="gridcell" style="width:141px;" tabindex="-1" data-columnid="gridcolumn-4007">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;"></div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4008" role="gridcell" style="width:120px;" tabindex="-1" data-columnid="gridcolumn-4008">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">NA</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4009" role="gridcell" style="width:85px;" tabindex="-1" data-columnid="gridcolumn-4009">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;"></div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4010" role="gridcell" style="width:358px;" tabindex="-1" data-columnid="gridcolumn-4010">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;"></div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4011" role="gridcell" style="width:127px;" tabindex="-1" data-columnid="gridcolumn-4011">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">No</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4012" role="gridcell" style="width:120px;" tabindex="-1" data-columnid="gridcolumn-4012">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">No</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4013" role="gridcell" style="width:134px;" tabindex="-1" data-columnid="gridcolumn-4013">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">December</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4014" role="gridcell" style="width:92px;" tabindex="-1" data-columnid="gridcolumn-4014">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">Yes</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4015" role="gridcell" style="width:57px;" tabindex="-1" data-columnid="gridcolumn-4015">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">No</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4016" role="gridcell" style="width:92px;" tabindex="-1" data-columnid="gridcolumn-4016">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;">No</div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4017" role="gridcell" style="width:568px;" tabindex="-1" data-columnid="gridcolumn-4017">
                    <div class="x-grid-cell-inner " style="text-align:left;padding-left:0px;background-position-x:0px;;"></div>
                </td>
                <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-4018" role="gridcell" style="width:106px;" tabindex="-1" data-columnid="gridcolumn-4018">
                    <div class="x-grid-cell-inner " style="text-
python-3.x selenium selenium-chromedriver
1个回答
0
投票

我将尝试重构以下行:

yp = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.XPATH, "//table/tbody/tr/td[6]")))[counter]

actionChains.double_click(yp).perform()

变得有些不同:

elements = WebDriverWait(driver, 10).until(
   EC.presence_of_all_elements_located((By.XPATH, "//table/tbody/tr/td[6]")))

yp = elements[counter]

# test to see if we get a StaleElementReferenceException here
print(yp.text)

actionChains.double_click(yp).perform()

如果这没有帮助,我建议您包括要尝试自动执行的表的HTML。一旦我们了解了表元素的外观,就可以对这种异常的发生方式有更多的了解,并进一步重构代码以修复该异常。

我的建议是重构XPath并将counter作为参数,而不是使用它从列表中获取元素。类似于:

yp = WebDriverWait(driver, 10).until(
   EC.presence_of_element_located((By.XPATH, "//table/tbody/tr[" + counter + "]/td[6]")))

这样,您只等待执行操作所需的元素,而不是整个列表。

© www.soinside.com 2019 - 2024. All rights reserved.