NoSuchElementException:消息:无法找到元素:// table [@ id ='tbl'] / tbody / tr [2] / td [2]试图通过Selenium和XPath查找元素

时间:2019-02-14 11:52:21

标签: selenium selenium-webdriver xpath webdriver xpath-1.0

我需要在TR中单击文本为 C1111-8PLTELAWZ 的元素。当我使用XPath "xpath("//table[@id='tbl']/tbody/tr[2]/td[2]").click()"时, 遇到以下错误:

Traceback (most recent call last):
  File "acs.py", line 17, in <module>
    qwe = browser.find_element_by_xpath("//table[@id='tbl']/tbody/tr[2]/td[2]").click()
  File "/users/hemgr/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/users/hemgr/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/users/hemgr/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/users/hemgr/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //table[@id='tbl']/tbody/tr[2]/td[2]

HTML代码:

<span id="UpdatePanel1">
    <table id="tbl" class="device_settings" x-ms-format-detection="none" style="width:100%;border-collapse:collapse;" cellspacing="0" cellpadding="0" border="0">
        <tbody>
            <tr>
                <th class="cell_header" style="width:20px;"><input type="checkbox" onclick="CheckAllRows(this);" id="IsCheckAllRows"></th>
                <th class="cell_header" onclick="SetOrder( 'manufacturer_name')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Manufacturer</span></th>
                <th class="cell_header" onclick="SetOrder( 'product_class_model')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Model name</span></th>
                <th class="cell_header" onclick="SetOrder( 'device_serial')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Serial</span></th>
                <th class="cell_header" onclick="SetOrder( 'device_created')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Created</span></th>
                <th class="cell_header" onclick="SetOrder( 'device_updated')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Updated</span><img src="../Images/down.gif"></th>
                <th class="cell_header_a" onclick="SetOrder( 'device_status')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Status</span></th>
            </tr>
            <tr isactive="Ok" rowid="9" onmouseout="RowOut(this, event)" onmouseover="RowOver(this, event);" onclick="RowClick('9',event);" style="cursor:pointer;">
                <td class="cell_inactive_item" onclick="CheckRow(this, event);" style="width:20px;cursor:default;" align="center"><input type="checkbox" onclick="CheckRow(this, event);"></td>
                <td class="cell_inactive_item" align="center">Cisco Systems Inc</td>
                <td class="cell_inactive_item" align="center">C1117-4PLTEEA</td>
                <td class="cell_inactive_item" align="center">FGL212891Z6</td>
                <td class="cell_inactive_item" align="center"><span style="white-space:nowrap;">2/15/2019 11:37:14</span></td>
                <td class="cell_inactive_item" align="center"><span style="white-space:nowrap;">2/15/2019 14:12:50</span></td>
                <td class="cell_inactive_item_a" align="center">0</td>
            </tr>
    </table>
</span>

3 个答案:

答案 0 :(得分:1)

1)如果要从表中获取“ C1111-8PLTELAWZ”值,则XPath不正确。应该是:

//span[@id='UpdatePanel1']//table[@id='tbl']/tbody/tr[2]/td[3]

2)最好在与元素进行交互之前使用显式等待使其可单击,以免出现问题:

from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='UpdatePanel1']//table[@id='tbl']/tbody/tr[2]/td[3]"))).click()

希望对您有帮助!

答案 1 :(得分:0)

您正在使用绝对xpath,这不是使用的好习惯:

您可以尝试:

//td[@class='cell_inactive_item' and contains(text(),'C1111-8PLTELAWZ')]

答案 2 :(得分:0)

可能您正在尝试在文本为模型名称click()下方的元素上调用<th>。为此,您可以使用以下Locator Strategy

driver.find_element_by_xpath("//table[@class='device_settings' and @id='tbl']//th[@class='cell_header']//span[.='Model name']//following::td[3]").click()