Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click an element within a table through Selenium Python [duplicate]

I would like to click the cell in the table ID "ContractDesc", "EEE" content:

The HTML page:

<table cellpadding="0" cellspacing="0" border="0" class="tablelist" id="table1" style="width: 80%; margin: 0px 0px 0px 0px;">
    <thead>
        <tr>
            <th style="width: 30%">
                AAA
            </th>
            <th>
                BBB
            </th>
            <th style="width: 40%">
                CCC
            </th>
        </tr>
    </thead><tbody>


    <tr id="1" onmouseout="fnMouseOut(1)" =="" ""="" onmouseover="fnMouseOver(1)" onclick="selectRow(this)" style="cursor: pointer; background-color: rgb(248, 248, 248);" projectid="111111">
        <td align="center" name="contno">
            DDD
        </td>
        <td name="ContractDesc">
            EEE
        </td>
        <td name="">
            FFF
        </td>
    </tr>

</tbody>
</table>

My code that is not working:

driver.find_element_by_xpath('//*[@id="1"]/td[2]').click()

and

driver.find_element_by_name("ContractDesc").click()

The error is:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="1"]/td[2]"}

like image 928
TZ J4 Avatar asked Jun 20 '26 05:06

TZ J4


1 Answers

Try this XPath:

driver.find_element_by_xpath("//td[@name='ContractDesc']").click()

Please check if the element is in an iframe, if yes then you need to switch the driver to the iframe by using: WebElement iFrame= driver.findElement(By.tagName("iframe")); and then driver.switchTo().frame(iFrame); and then you need to click the element by the given xpath and if you want to switch to the default context then you can use driver.switchTo().defaultContent();

like image 61
Sameer Arora Avatar answered Jun 23 '26 00:06

Sameer Arora