Selenium: Element Not Found for existing element
It should be By.className instead of cssSelector.
Example:
WebElement parentElement = driver.findElement(By.className("rose-button"));
If you want to use cssSelector, it should look like:
driver.findElement(By.cssSelector("button[class='app-button rose-button min-width']"));
Another way is using xpath:
driver.findElement(By.xpath("//div[@class='app-button rose-button min-width']"));
And as DebanjanB mentioned, you can add webdriverwait also, if there is some kind of loading delay.
How to click a button in an automated test using Selenium and Java
To invoke click()
on the element you can use either of the following Locator Strategies:
Using linkText:
driver.findElement(By.linkText("Login")).click();
Using cssSelector:
driver.findElement(By.cssSelector("a[href='/login']")).click();
Using xpath:
driver.findElement(By.xpath("//a[@href='/login' and text()='Login']")).click();
Best practices
As you are invoking click()
ideally you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies:
Using linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Login"))).click();
Using cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='/login']"))).click();
Using xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/login' and text()='Login']"))).click();
Reference
You can find a couple of relevant discussions in:
- NoSuchElementException, Selenium unable to locate element
- Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id='login-email']
Related Topics
How to Use an Arraylist as a Prepared Statement Parameter
Java.Lang.Illegalstateexception: Scanner Closed
How to Populate a Drop Down with a List Using Thymeleaf and Spring
Android - How to Take Screenshot Programmatically
Android Studio Error: "Manifest Merger Failed: Apps Targeting Android 12"
How to Convert Firebase Data to Java Object...
Deprecated Managedquery() Issue
Convert Utc into Local Time on Android
Android:Java.Lang.Outofmemoryerror:
Meaning of the Import Statement in a Java File
Java Interfaces/Implementation Naming Convention
How to Log into Facebook Programmatically Using Java
Map.Clear() VS New Map:Which One Will Be Better
How to Know Whether I am in a Call on Android
Opencv Template Matching Example in Android