1

I need to click on the element with text as C1111-8PLTELAWZ within the TR. When I used the XPath "xpath("//table[@id='tbl']/tbody/tr[2]/td[2]").click()",` facing the below error:

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]

The HTML code:

<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>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Hema
  • 11
  • 2

3 Answers3

1

1) Your XPath is not correct if you want the "C1111-8PLTELAWZ" value from the table. It should be:

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

2) It's better to use explicit wait to be clickable of the element before interaction with it (to avoid problems):

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

Hope it helps you!

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
0

You are using absolute xpath, which is not good practice to use:

You may try :

//td[@class='cell_inactive_item' and contains(text(),'C1111-8PLTELAWZ')]
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
0

Possibly you are trying to invoke click() on the element which is just below the <th> with text as Model name. To achieve that you can use the following 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()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352