Aasil
Aasil

Reputation: 103

Selenium: Element Not Found for existing element

So I have the following HTML Tag I want to fetch with Selenium:

<button data-v-48e2f75a="" class="app-button rose-button min-width">Sign in</button>

I am trying to fetch it like so:

...
browser.findElement(By.cssSelector("app-button rose-button min-width")).click();

When I run this it always returns the following exception:

org.openqa.selenium.NoSuchElementException: Unable to locate element: app-button rose-button min-width

This is one of the methods I've tried already however it has not worked either:

browser.findElement(By.cssSelector("app-button rose-button min-width")).click();

Upvotes: 0

Views: 458

Answers (3)

Ledjon
Ledjon

Reputation: 125

The css selector is wrong. Try it in this way:

  • browser.findElement(By.cssSelector(".app-button.rose-button.min-width")).click();

Upvotes: 2

Abhinaba Chakraborty
Abhinaba Chakraborty

Reputation: 3671

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.

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193338

To click on the element with text as Sign in you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.app-button.rose-button.min-width"))).click();
    
  • Using xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='app-button rose-button min-width' and text()='Sign in']"))).click();
    

Reference

You can find a couple of detailed discussions in:

Upvotes: 0

Related Questions