Karthi .M
Karthi .M

Reputation: 29

How do i click a Button inside a Table using Selenium(Python)

<table _ngcontent-c19="" class="table">
  <tbody _ngcontent-c19="">
    <tr _ngcontent-c19=""><!---->
      <td _ngcontent-c19="" class="ng-star-inserted">
        <div _ngcontent-c19="" class="span3" style="height: auto;">
          <div _ngcontent-c19="" class="text-center" style="visibility: visible;">
            <button _ngcontent-c19="" class="b1" type="submit">Click This</button>
          </div><!---->
        </div>
      </td>
    </tr>
  </tbody>
</table>

I'm trying to click a button inside a table using SELENIUM.

The Code I've Written is

driver.find_element_by_xpath("//*[contains(getText(),'Click This')]").click()

and

driver.find_element_by_class_name('b1').click()

they both throw an Element not Found Exception

Upvotes: 2

Views: 3772

Answers (2)

Hamza Torjmen
Hamza Torjmen

Reputation: 270

Because your code runs faster than your browser that's why you need to tell him to wait until the element is visible and clickable.

button = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "//button[text()='Click This']"))
button.click()

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

you can try with this xpath :

wait = WebDriverWait(driver,30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Click This') and @class='b1']"))).click()

Note that you will have to import :

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

Upvotes: 1

Related Questions