How to find button element with webdriver?

I have the following code for a button :

<div class="buttons">
<button class="btn dialog-confirm btn-primary" style="margin-left: 4px;">Confirm</button>
<button class="btn dialog-cancel" style="margin-left: 4px;">Cancel</button>

There are two buttons on is Confirm and another is Cancel I can find the button with XPath but I don't want to use XPath. Is there another way to find the button element in this case?

I tried this:

driver.findElement(By.className("btn dialog-confirm btn-primary")).click();

It did not find the button Thank you for your help

Just check for a single dialog-confirm class:


Or, use a CSS Selector:


How to find button with Selenium by its text inside (Python)?, You can use ActionChains to move to the element before clicking on it from selenium.webdriver.common.action_chains import ActionChains� Find Element command returns the web element that matches the first most element within the web page. Find Elements command returns a list of web elements that match the criteria. Find Element command throws NoSuchElementException if it does not find the element matching the criteria.

Other ways using cssSelector:

  1. Use full attribute i.e.:

    driver.findElement(By.cssSelector("button[class='btn dialog-confirm btn-primary']"))

  2. Use part of attribute i.e.:


Selenium find button element, Login button is available on login screen. add IDs to each elements. because, sometimes the UI or the text may change, then you had to modify your test script. If you encounter NoSuchElementException() while finding elements, it means that the element is not found in the page at the point the Web driver accessed the page. Check your locator again using Firepath or Inspect Element in Chrome. Check whether the value you used in the code is different from the one for the element in Firepath now.

Added to alecxe and master slave's answer. It would be more specific if it is clicked by the button text, which is also easier to understand. Find the snippet for button click with xpath below.


Selenium: How to identify the button WebElement, Web Driver provides the following two methods to find the elements. findElement () – finds a single web element and returns as a WebElement� That’s why we’ve used the Webdriver findElements command to get the list of web elements. Use element value for selection: One of the intuitive ways to select the Radio Buttons/CheckBoxes is by toggling their Values.

vote up for alecxe, your attempt was wrong on two accounts, when matching on multiple classes you should use By.cssSelector, and when they are set on the same element, you concatenate them with a dot, like


Selenium Form WebElement: TextBox, Submit Button, sendkeys , Locator is a command that tells Selenium IDE which GUI elements ( say Text Box, Buttons, Check Boxes etc) its needs to operate on. to find the html element. To get the path, we can use chrome development tools (press F12). We take the pointer in devtools and select the html button we are interested in. The path will then be shown, as the example screenshot: Find element by xpath, using chrome dev tools

this worked for me:


Locators in Selenium IDE: CSS Selector, DOM, XPath, Link Text, ID, To find multiple elements (these methods will return a list): Example usage: from import By driver.find_element(By.XPATH This will give the “Login” button as it occurs before the “Clear” button: continue� Tests of this sort should test the app the way a user would use it -- that is, they should test as much as possible based on UI, not element IDs. Therefore, they should find buttons based on text, not ID.

4. Locating Elements — Selenium Python Bindings 2 documentation, Name() Similarly, you can find the name of a web element by inspecting the HTML and use it to locate the web element using Selenium automation testing. To use the Selenium click button with Name locator, you need to address it in a similar way. Hidden elements are the elements which are not displayed on the Web-Pages, however, it is available in the HTML DOM structure. Elements are hidden on the web-pages due to below-mentioned reasons - They have a CSS display value of none.

Test Automation With Selenium Click Button Method(Examples), How to find a list of elements that end with a particular string by using Selenium with Ruby. The problem is that, the end_with method . The WebDriver Protocol provides several selector strategies to query an element. WebdriverIO simplifies them to keep selecting elements simple. Please note that even though the command to query elements is called $ and $$, they have nothing to do with jQuery or the Sizzle Selector Engine.

Selenium can't find button, Selenium Find element by text which is used to locate a web element using its text value. The text value can be fully matched or partially� In this tutorial, we will learn about CSS Selector and create CSS selectors manually using different HTML attributes of the web elements. For fetching the HTML information of the web elements we will use a firebug or developer tool. (For details on downloading and usage of firebug/developer tool check out tutorial - Finding web elements in