NoSuchElementException: Сообщение: невозможно найти элемент: // table [@ id = 'tbl'] / tbody / tr [2] / td [2], пытающийся найти элемент через Selenium и XPath - PullRequest
0 голосов
/ 14 февраля 2019

Мне нужно нажать на элемент с текстом как C1111-8PLTELAWZ в TR.Когда я использовал 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 голосов
/ 14 февраля 2019

Вы используете абсолютный xpath, который не рекомендуется использовать:

Вы можете попробовать:

//td[@class='cell_inactive_item' and contains(text(),'C1111-8PLTELAWZ')]
0 голосов
/ 16 февраля 2019

Возможно, вы пытаетесь вызвать click() для элемента, который находится чуть ниже <th> с текстом как Название модели .Для этого вы можете использовать следующую стратегию Locator :

driver.find_element_by_xpath("//table[@class='device_settings' and @id='tbl']//th[@class='cell_header']//span[.='Model name']//following::td[3]").click()
0 голосов
/ 14 февраля 2019

1) Ваш XPath неверен, если вам нужно значение "C1111-8PLTELAWZ" из таблицы.Это должно быть:

//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()

Надеюсь, это поможет вам!

...