Varun
Varun

Reputation: 65

Python Selenium Click a button with class

I'm new to python selenium and I'm trying to click on a button which has the following HTML structure:

<div class="wpsafe-top text-center"> 
  <h1><strong><span style="color: #ce2525;">Click on Submit to Get Link</span></strong></h1><br>
  <form action="https://url.com/post" method="post">
    <input type="hidden" name="getlink" value="NVudM3E">
    <input type="hidden" name="newwpsafelink" value="eyJsaW5rIjoiTlZ1ZE0zRSIsImFkczEiOiIiLCJhZHMyIjoiIiwibG9nbyI6IiIsImltYWdlMSI6Imh0dHA6XC9cL3RlY2hjb2RlY3MuY29tXC93cC1jb250ZW50XC91cGxvYWRzXC8yMDE5XC8xMlwvZ2VuLnBuZyIsImltYWdlMiI6Imh0dHA6XC9cL3RlY2hjb2RlY3MuY29tXC93cC1jb250ZW50XC91cGxvYWRzXC8yMDE5XC8xMlwvcGx6LnBuZyIsImltYWdlMyI6Imh0dHA6XC9cL3RlY2hjb2RlY3MuY29tXC93cC1jb250ZW50XC91cGxvYWRzXC8yMDE5XC8xMlwvY2xrLnBuZyIsImxpbmtyIjoiaHR0cHM6XC9cL3RlY2hjb2RlY3MuY29tP3NhZmVsaW5rX3JlZGlyZWN0PTUlNUIlOUQzcSIsImRlbGF5dGV4dCI6IjxiPjxoMiBzdHlsZT0nY29sb3I6cmVkJz5Mb2FkaW5nIExpbmsgLi4uIFdhaXQgPHNwYW4gaWQ9XCJ3cHNhZmUtdGltZVwiPjEyPFwvc3Bhbj4gU2Vjb25kczxcL2gyPjxcL2I+IiwiZGVsYXkiOiIxMiIsImFkYiI6IjIiLCJhZGIxIjoiIiwiYWRiMiI6IiJ9">
    <input class="btn btn-primary" type="submit" value="Submit">
  </form> 
</div>

I have tried this to click on Submit Button.

driver.find_element_by_css_selector('btn btn-primary').click()

But i always end up with error

Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn btn-primary"}

Upvotes: 3

Views: 7051

Answers (1)

Guy
Guy

Reputation: 50809

find_element_by_css_selector('btn btn-primary') will look fir an element with tag btn-primary that has ancestor element with tag btn.

You need to tell the driver those are classes with .

driver.find_element_by_css_selector('.btn.btn-primary').click()

Or explicitly

driver.find_element_by_css_selector('[class="btn btn-primary"]').click()

Upvotes: 4

Related Questions