Search code examples
selenium-webdriverhtml-tableselenium-chromedriver

Get the 3rd element (table) on webpage using Selenium find element


table = driver.find_element(By.XPATH, "//table")

This is working but finds the first table on webpage, there are three on the page and I want the third, how can I find the third table element, or otherwise uniquely identify and find the correct table?

<table class="sortable" id="results" cellspacing="4" cellpadding="0" align="center" border="0">
    <caption>...</caption>
    <thread>...</thread>
    <tbody>...</tbody>
</table>
</div>
<br>
 <div>
<table class="sortable" id="results" cellspacing="4" cellpadding="0" align="center" border="0">

Solution

  • (//table)[1] - This will locate the first table in the DOM, (//table)[2]this will locate the second table in the DOM and so on.

    Try the below to locate third table:

    third_table = driver.find_element(By.XPATH, "(//table)[3]")