Selenium: Element Not Found for existing element - java

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();

The css selector is wrong. Try it in this way:
browser.findElement(By.cssSelector(".app-button.rose-button.min-width")).click();

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.

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:
NoSuchElementException, Selenium unable to locate element
Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[#id='login-email']

Related

Unable to locate element: {"method":"xpath","selector":"//li[#id="tablist1-tab3"]"} error using Selenium and Java

I have received this error for several times:
Unable to locate element: {"method":"xpath","selector":"//li[#id="tablist1-tab3"]"}
Code that I have used is:
options.addArguments("--headless");
options.addArguments("window-size=1200x900");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement tab = driver.findElement(By.xpath("//li[#id=\"tablist1-tab3\"]"));
tab.click();
Can someone help me with this error?
You need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li#tablist1-tab3"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#id=\"tablist1-tab3\"]"))).click();
WebElement tab = driver.findElement(By.xpath('//li[#id="tablist1-tab3"]'));
try this
Make sure the element is not inside an iFrame. If it is, then you need to first find the iFrame element and use Selenium's switchTo(). After that you will be able to locate elements inside the iframe.

Selenium did not accept my xpath expression

I tried this xpath to retrieve a button
By addGhostButton = By.xpath("//button[#data-test-id=\"order\"]");
Selenium did not find it, I had this error invalid selector: Unable to locate an element with the xpath expression
Maybe the syntax is wrong ? any help please?
You haven't provided the complete error stack trace and the relevant HTML. However, the xpath you constructed is pretty much valid and legit. Hence you shouldn't see invalid selector error but Unable to locate an element with the xpath expression is pretty much possible.
The desired element looks to be a dynamic element so to locate the element you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[data-test-id='order']")));
Using xpath:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#data-test-id='order']")));
Try with this xpath //button[Contains(text(),"Précommande maintenant")]`

Not able to select a checkbox in Selenium Webdriver with java

I'm trying to select a checkbox but I'm not able to and didn't find any solution in other threads.
Here is my HTML code for drop down list:
code:
#FindBy(css="input.ng-valid.ng-dirty.ng-touched")
WebElement chrgAllocFee;
chrgAllocFee.click();
but it is not working where as other checkboxes are working when I define and use in same way. I found that the difference as compared to others is that here html code has additional line with
Please suggest how to define so that it can be recognised.
Error getting:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"input.ng-valid.ng-dirty.ng-touched"}
(Session info: chrome=69.0.3497.92)
The desired element is an Angular element so you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ng-valid.ng-dirty.ng-touched[name='ChargeAllocationFee']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='ng-valid ng-dirty ng-touched' and #name='ChargeAllocationFee']"))).click();
I found solution:
I used javascript for clicking that field and it works fine and here is the code:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", CamWizOR.chrgAllocFee);
CamWizOR.chrgAllocFee -> WebElement name
#FindBy(xpath="//input[#name='ChargeAllocationFee' and #type='checkbox']")
WebElement chrgAllocFee;

Which locator I can use to click the span element through selenium

Which locator can I use for the below?
<span class="tile-name ng-binding">Payment Partner</span>
Update from OP's comments:
Code trial 1:
driver.findElementByXPath("//span[text()='Payment Partner']").click();
Error:
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible;
Code trial 2:
driver.findElementByCssSelector("Payment Partner").click();
Error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"Payment Partner"}
Code trial 3:
WebDriverWait wait = new WebDriverWait(driver, 20);wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath ("//span[text()='Payment Partner']')")));
As per the HTML you have shared to to find the <span> tag, as the element is an Angular element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solution:
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='tile-name ng-binding' and contains(.,'Payment Partner')]"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.tile-name.ng-binding"))).click();
Update
As per the error update to invoke click() you can use the following solution:
xpath:
WebElement myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='tile-name ng-binding' and contains(.,'Payment Partner')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", myElement);
You can find a detailed discussion in Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click:
Following are variance that you can use for above HTML line :
Xpath : //span[text()='Payment Partner']
Xpath : //span[#class='tile-name ng-binding']
Xpath : //span[contains(#class,'tile-name')]
Xpath : //span[contains(#class,'ng-binding')]
Xpath : //span[contains(#class,'tile-name') and text()='Payment Partner']
CssPath : span[class='tile-name ng-binding']
I can add more If there would be parent element.
Hope this is helpful.

How to find the button element through Selenium webdriver and xpath

snapshot
<button class="add-school btnEditDashboard btn btn-primary pull-right" ng-click="CreateNewSchool()" name="AddSchool" type="submit">Add School</button>
Tried xpath also. (\\html/body/div[2]/div/div[2]/div/div/div[1]/div/div[2]/button)
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"tag name","selector":"AddSchool"}
try with this code :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Add School' and #name='AddSchool' and #ng-click='CreateNewSchool()']"))).click();
If it is in iframe , please switch focus of driver to respective iframe and then try to interact with it.
AddSchool is the name attribute, not the tag name (which will be button in your case). Try
findElement(By.name("AddSchool"))
To locate the element instead of using an absolute xpath it would be optimum to use a relative xpath. Now, as per the HTML you have shared the element is an Angular element so you have to induce WebDriverWait for the element to be clickable as follows:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='add-school btnEditDashboard btn btn-primary pull-right' and #name='AddSchool']"))).click();
Since the HTML is not shared, it could be 2 things
Iframe: You need to change your drivers focus to the iframe the element is in.
https://www.guru99.com/handling-iframes-selenium.html
Selenium is executing before the element is visible. Use Explicit Wait to wait for the element to be interactable before clicking it. Understand all the type of wait conditions through http://www.seleniumeasy.com/selenium-tutorials/webdriver-wait-examples
Try this xpath:
//button[#ng-click='CreateNewSchool()']

Categories

Resources