我有一个包含4行的表格。每行的第一列中都有一个复选框。 4个复选框,因为有4行。名称,地址,DOB,电话的复选框
我想选中所有复选框。我的代码执行此操作,因为我可以看到在运行Selenium Python代码时勾选复选框。 勾选所有复选框后,它会抛出错误:
StaleElementReferenceException: Message: Element is no longer valid
我的代码方法实现如下:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
def click_all_variables_from_variables_section(self):
#checkboxes = self.driver.find_elements_by_css_selector("#reporting_add_report_tab_manual_ct_vars_usn input[type='checkbox']")
#checkboxes = WebDriverWait(self.driver, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "'#reporting_add_report_tab_manual_ct_vars_usn input[type='checkbox']'")))
checkboxes = self.driver.find_elements(By.XPATH, '//table[@id="reporting_add_report_tab_manual_ct_vars_usn"]//input[@type="checkbox"]')
for checkbox in checkboxes:
checkbox.click()
return self
从我的代码中,您可以看到我尝试过的其他内容的一些评论,例如WebDriverWait。当我尝试使用WebDriverWait时,它在一个循环中停留了很长时间。我不得不靠近浏览器退出。
HTML是:
<table id="reporting_add_report_tab_manual_ct_vars_usn" class="GLKP2TGBJE" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
<thead aria-hidden="false">
<colgroup>
<tbody style="">
<tr class="GLKP2TGBCD GLKP2TGBJD" __gwt_subrow="0" __gwt_row="0">
<td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED GLKP2TGBKD">
<div __gwt_cell="cell-gwt-uid-488" style="outline-style:none;">
<input type="checkbox" tabindex="-1" />
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBKD">
<div __gwt_cell="cell-gwt-uid-489" style="outline-style:none;">Name</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBKD">
<div __gwt_cell="cell-gwt-uid-490" style="outline-style:none;">
<select tabindex="-1">
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBKD">
<div __gwt_cell="cell-gwt-uid-491" style="outline-style:none;">
<select tabindex="-1">
<option selected="selected" value="" />
</select>
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD GLKP2TGBKD">
<div __gwt_cell="cell-gwt-uid-492" style="outline-style:none;">
<input type="text" tabindex="-1" value="" />
</div>
</td>
</tr>
<tr class="GLKP2TGBCE" __gwt_subrow="0" __gwt_row="1">
<td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBED">
<div __gwt_cell="cell-gwt-uid-488" style="outline-style:none;">
<input type="checkbox" tabindex="-1" />
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDE">
<div __gwt_cell="cell-gwt-uid-489" style="outline-style:none;">Address</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDE">
<div __gwt_cell="cell-gwt-uid-490" style="outline-style:none;">
<select tabindex="-1">
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDE">
<div __gwt_cell="cell-gwt-uid-491" style="outline-style:none;">
<select tabindex="-1">
<option selected="selected" value="" />
</select>
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBOD">
<div __gwt_cell="cell-gwt-uid-492" style="outline-style:none;">
<input type="text" tabindex="-1" value="" />
</div>
</td>
</tr>
<tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="2">
<td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED">
<div __gwt_cell="cell-gwt-uid-488" style="outline-style:none;">
<input type="checkbox" tabindex="-1" />
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDD">
<div __gwt_cell="cell-gwt-uid-489" style="outline-style:none;">DOB</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDD">
<div __gwt_cell="cell-gwt-uid-490" style="outline-style:none;">
<select tabindex="-1">
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDD">
<div __gwt_cell="cell-gwt-uid-491" style="outline-style:none;">
<select tabindex="-1">
<option selected="selected" value="" />
</select>
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD">
<div __gwt_cell="cell-gwt-uid-492" style="outline-style:none;">
<input type="text" tabindex="-1" value="" />
</div>
</td>
</tr>
<tr class="GLKP2TGBCE" __gwt_subrow="0" __gwt_row="3">
<td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBED">
<div __gwt_cell="cell-gwt-uid-488" style="outline-style:none;">
<input type="checkbox" tabindex="-1" />
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDE">
<div __gwt_cell="cell-gwt-uid-489" style="outline-style:none;">Phone</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDE">
<div __gwt_cell="cell-gwt-uid-490" style="outline-style:none;">
<select tabindex="-1">
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDE">
<div __gwt_cell="cell-gwt-uid-491" style="outline-style:none;">
<select tabindex="-1">
<option selected="selected" value="" />
</select>
</div>
</td>
<td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBOD">
<div __gwt_cell="cell-gwt-uid-492" style="outline-style:none;">
<input type="text" tabindex="-1" value="" />
</div>
</td>
</tr>
</tbody>
<tbody style="display: none;">
<tr>
<td align="center" colspan="5">
<div>
<div style="width: 100%; height: 100%; padding: 0px; margin: 0px; display: none;" aria-hidden="true">
<div style="width: 100%; height: 100%; display: none;" aria-hidden="true">
<div class="gwt-Label">No data to display.</div>
</div>
</div>
<div style="width: 100%; height: 100%; padding: 0px; margin: 0px;" aria-hidden="false">
<div class="GLKP2TGBBE" style="width: 100%; height: 100%;" aria-hidden="false">
<div class="gwt-Label">Loading...</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
<tfoot style="" aria-hidden="false">
</table>
完整的错误跟踪是:
Traceback (most recent call last):
File "C:\Webdriver\ClearCore\TestCases\ReportingPage_TestCase.py", line 53, in test_add_matches_report
reports_page.click_all_variables_from_variables_section()
File "C:\Webdriver\ClearCore\Pages\reports.py", line 75, in click_all_variables_from_variables_section
checkbox.click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 65, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 402, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 175, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: Element is no longer valid
我还尝试先等待表加载,然后单击复选框。 我的代码是:
WebDriverWait(self.driver, 20).until(EC.presence_of_all_elements_located((By.ID, '//table[@id="reporting_add_report_tab_manual_ct_vars_usn"]')))
checkboxes = self.driver.find_elements(By.XPATH, '//table[@id="reporting_add_report_tab_manual_ct_vars_usn"]//input[@type="checkbox"]')
for checkbox in checkboxes:
checkbox.click()
这个抛出TimeoutException
如何解决这个问题,点击所有复选框,不会出现任何错误?
谢谢, 里亚兹