3

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]"}

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
TZ J4
  • 33
  • 1
  • 6

3 Answers3

2

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

Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
  • Thank you sir, but it still error "Unable to locate element:" – TZ J4 Feb 01 '19 at 09:28
  • Please check if its in an iframe and if yes, 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 `driver.find_element_by_xpath("//td[@name='ContractDesc']").click()` and if you want to switch to the default context then you can use `driver.switchTo().defaultContent();` Please try this and let me know if it works. I am editing my answer as well. – Sameer Arora Feb 01 '19 at 15:09
1

You can use xpath below to select by name and text, normalize-space remove all whitespaces:

//td[@name='ContractDesc' and normalize-space(.)='EEE']
Sers
  • 12,047
  • 2
  • 12
  • 31
1

To click() on the element with text as EEE you can use either of the following solutions:

  • Using CSS_SELECTOR:

    driver.find_element_by_css_selector("table.tablelist#table1 tr#1 td[name='ContractDesc']").click()
    
  • Using XPATH:

    driver.find_element_by_xpath("//table[@class='tablelist' and @id='table1']//tr[@id='1']//td[@name='ContractDesc' and normalize-space()='EEE']").click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you so much, but both are not work CSS return error "A18-0110" and XPATH return "Unable to locate element:" – TZ J4 Feb 01 '19 at 09:33