Selenium test fails with java - java

I use Selenium with cucumber in Java and when trying to do that :
WebElement MyAccountLink = driver.findElement(By.className("btn-outlined-white_medium_block"));
MyAccountLink.click();
I got this error:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"class name","selector":"btn-outlined-white_medium_block"}
How can i solve it ?
Thanks.

use 'XPath' or 'cssSelector' instead of 'className', because it doesn't matters which locator you are using. you need to find the element and do automation on that element.
WebElement MyAccountLink = driver.findElement(By.xpath("right click on element and copy xpath and paste it here"));
MyAccountLink.click();
Hope XPath will work in any case.

Related

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

Java selenium using xpath : NoSuchElementException

Now I'm doing browser test with selenium(java).
However, there are some problems with xpath.
I tried below code.
webDriver.findElement(By.xpath("//button[#onclick='addUserWf();return false;']")).click();
with web element
<button class="btn-etc btn-object-add" onclick="addUserWf();return false;">...</button>
Maybe you can refer, spring boot print :
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[#onclick='addUserWf();return false;']"}
I can see above element(It means element displayed and visible), so I can't understand.
Add : I tried this, but result was same.
webDriver.findElement(By.xpath("//button[#class='btn-etc btn-object-add']")).click();
Someone knows this?
Try below xpath:
webDriver.findElement(By.xpath("//button[#onclick=\"addUserWf();return false;\"]")).click();
or
webDriver.findElement(By.xpath("//button[starts-with(#onclick='addUserWf')]")).click();
And its always good practice to use webdriver wait before clicking on any button.
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath(XPATH)))

How to find a certain web-element on the web-page

Unable to locate the 'Number of instances' field on https://cloud.google.com/products/calculator/ by the customized XPath with WebDriver (Java-powered)
I have tried the xpath: //input[#name = "quantity" and #id="input_52"]
It works fine with Ctrl + F with the Chrome inspect code feature, but not with WebDriver
Here is the stacktrace message in Idea:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#name = "quantity" and #id="input_52"]"}
Thank You for Your kind help in advance
element you trying to operate on is inside frame you have to select frame then you can perform operation on element do the following step to reach to element inside iframe(id of iframe is - id="idIframe") -
driver.switch_to.frame('idIframe');
driver.find_element_by_xpath("//input[#name = 'quantity' and #id='input_52']");

how to locate ::Before element in Selenium Java

unable to click on this specific "X" element
try with following xpath, It may works.
//*[#id='ngdialog1']//*[#class='ngdialog-close']
In java
driver.findElement(By.xpath("//*[#id='ngdialog1']//*[#class='ngdialog-close']").click();
try using following code:
driver.findElement(By.xpath("//table[#id='ngdialog1']//table//following-sibling::div[1]").click();

JAVA - Selenium findElement exception

I'am writting small application for automated posting on Facebook. To achive that I copied XPath of "posting input field" from my profile (like in screenshoot):
This is what has been copied:
//*[#id="js_3m"]/div[1]/div[1]/div[2]/div/div/div/div/div/div/div/div/div/span/span
Now i have created following line of code:
WebElement element = driver.findElement(By.xpath("//*[#id=\"js_3m\"]/div[1]/div[1]/div[2]/div/div/div/div/div/div/div/div/div/span/span"));
Which causes:
no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="js_3m"]/div[1]/div[1]/div[2]/div/div/div/div/div/div/div/div/div/span/span"}
Is this approach correct to achieve that?
Thanks for help :)

Categories

Resources