Python中的Webdriver如何等待HTML表加载

时间:2015-09-21 14:29:31

标签: python-2.7 selenium selenium-webdriver

我正在使用Selenium Webdriver和Python。我在如何等待表格完成加载方面遇到了麻烦。

网页显示列表(名称,变量,Feed等)。每行都有每列的数据。例如。露西,姓名,数据源。我正在检查if语句中每行的数据是否正确。我需要等待表中的所有行加载,以避免Element不再有效。

等待整个表加载的最佳方法是什么? 我尝试过使用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>

谢谢Riaz

1 个答案:

答案 0 :(得分:1)

我设法解决了这个问题。只需等待表容器加载。不要等待行和列。 我还将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...