gpt4 book ai didi

python-2.7 - Python 中的 Webdriver 如何等待 HTML 表格加载

转载 作者:行者123 更新时间:2023-12-02 03:21:59 28 4
gpt4 key购买 nike

我正在将 Selenium Webdriver 与 Python 结合使用。我在如何等待表格完成加载时遇到了问题。

网页显示列表(名称、变量、提要等)。每行都有每一列的数据。例如。露西,姓名,数据馈送。我正在检查 if 语句中每一行的数据是否正确。我需要等待表中的所有行都被加载以避免 Element is no longer valid。

等待整个表加载的最佳方式是什么?我试过使用 WebdriverWait。我认为我的语法在错误的地方。

我的代码是:

def is_mappings_details_saved(self, name, dataset, feed, variable):
try:
#self.driver.implicitly_wait(10)
wait = WebDriverWait(self.driver, 20)
table_id = wait.until(EC.presence_of_element_located((By.ID, 'data_configuration_mappings_ct_fields_body')))
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
time.sleep(1)
# Get the columns
col_name = row.find_elements(By.TAG_NAME, "td")[1] # This is the Name column
col_variable = row.find_elements(By.TAG_NAME, "td")[2] # This is the Variable column
col_feed = row.find_elements(By.TAG_NAME, "td")[3] # This is the Feed column
col_data_category = row.find_elements(By.TAG_NAME, "td")[4] # This is the Data Category column
col_dataset = row.find_elements(By.TAG_NAME, "td")[6] # This is the Dataset column
col_datamap = row.find_elements(By.TAG_NAME, "td")[7] # This is the Datamap column
if (col_name.text == name) and (col_variable.text == variable) and (col_feed.text == feed) and (col_data_category.text == "Main") and (col_dataset.text == dataset) and (col_datamap.text == self.datamap_name):
return True
return False
except NoSuchElementException, e:
return False

错误是:

raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: Element is no longer valid

错误处突出显示的行是:

rows = table_id.find_elements(By.TAG_NAME, "tr")

我等待表加载,然后查找行。如果我有整张表,是否不需要查找行?

HTML 片段是:

<table id="data_configuration_mappings_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GOFU2OVFG GOFU2OVMG" __gwt_subrow="0" __gwt_row="0">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG GOFU2OVNG">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<div __gwt_cell="cell-gwt-uid-418" style="outline-style:none;">
<span class="linkhover" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Name</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<div __gwt_cell="cell-gwt-uid-419" style="outline-style:none;">
<span class="" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Name</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<div __gwt_cell="cell-gwt-uid-420" style="outline-style:none;">
<span class="" title="crm" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">crm</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<div __gwt_cell="cell-gwt-uid-421" style="outline-style:none;">
<span class="" title="Main" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Main</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<div __gwt_cell="cell-gwt-uid-422" style="outline-style:none;">
<span class="" title="TITLE + FNAME + SNAME" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">TITLE + FNAME + SNAME</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<div __gwt_cell="cell-gwt-uid-423" style="outline-style:none;">
<span class="" title="CRM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">CRM</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH GOFU2OVNG">
</tr>
<tr class="GOFU2OVEH" __gwt_subrow="0" __gwt_row="1">
<td class="GOFU2OVEG GOFU2OVFH GOFU2OVHG">
<div __gwt_cell="cell-gwt-uid-417" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVFH">
<div __gwt_cell="cell-gwt-uid-418" style="outline-style:none;">
<span class="linkhover" title="DOB" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">DOB</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVFH">
<td class="GOFU2OVEG GOFU2OVFH">
... etc
</tr>
</tbody>
</table>

谢谢,里亚兹

最佳答案

我已经设法解决了。只需等待表容器加载即可。不等待行和列。我还将 self.driver.implicitly_wait(20) 放在基类中

class BasePage(object):

def __init__(self, driver):
self.driver = driver
self.driver.implicitly_wait(20)

class MappingsPage(BasePage):
def is_mappings_details_saved(self, name, dataset, feed, variable):
table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_mappings_ct_fields_body')))
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
# Get the columns
col_name = row.find_elements(By.TAG_NAME, "td")[1]
col_variable = row.find_elements(By.TAG_NAME, "td")[2]
etc...

关于python-2.7 - Python 中的 Webdriver 如何等待 HTML 表格加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32697811/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com