尽管我在每个循环的开头都在搜索元素,但循环中的陈旧元素仍然存在



我正在编写一个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:userso34639appdatalocalprogramspythonpython37-32libsite-packagesseleniumwebdrivercommonaction_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:userso34639appdatalocalprogramspythonpython37-32libsite-packagesseleniumwebdrivercommonactionsaction_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:userso34639appdatalocalprogramspythonpython37-32libsite-packagesseleniumwebdriverremotewebdriver.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:userso34639appdatalocalprogramspythonpython37-32libsite-packagesseleniumwebdriverremoteerrorhandler.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-

我会尝试重构以下行:

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

变成了一个有点不同的东西:

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

此代码使用一个XPath,该XPath将counter作为行索引,并等待您要执行操作的特定行,而不是等待所有行,然后从返回的列表中选择行。

这只是比您以前的代码所做的稍微具体一点。我们使用counter + 1是因为XPath查询中的WebElement列表是基于1的,而不是像Python列表那样基于0的。

最新更新