JAVA - Selenium findElement exception - java

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 :)

Related

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

Selenium test fails with 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.

Trying to click link via selenium in java

Using selenium in java. We have a series of links that look approximately like:
<a href='javascript:newWin("/ABC")'>ABC</a>
We're trying to click that link. There's no id unfortunately. We've tried a few things along the lines of the following.
driver.findElement(By.xpath("(//[contains(#href, 'javascript:newWin')])")).click();
Which results in:
Unable to locate an element with the xpath expression
(//[contains(#href, 'javascript:newWin')]) because of the following
error: [object Error] (WARNING: The server did not provide any
stacktrace information)
We're trying to figure out how to XPath to that anchor, and click it. We can't get an ID on it (at least not yet).
Try this one
driver.findElement(By.xpath("//a[text()='ABC']").click();
I think you're XPATH is malformed, but you're on the right track. Try:
driver.findElement(By.xpath("//a[contains(#href, 'javascript:newWin')]")).click();

Selenium WebDriver Java cssSelector Span

I am trying to click on a link using Selenium WebDriver in Java. My Java:
driver.findElement(By.cssSelector("span[data-seleniumid=\"Address0\"]")).click();
The HTML on my page looks like this:
<span data-seleniumid="Address0" class="ATAddressLine">1 The Road, Town, City, Postcode</span>
The error in Eclipse is:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"span[data-seleniumid=\"Address0\"]"}
Thanks
Instead of trying to escape the inner double quotes, just use a single quote instead.
driver.findElement(By.cssSelector("span[data-seleniumid='Address0']")).click();
I would try a different selector like "span.ATAddressLine". Not sure if webdriver likes your attribute "data-seleniumid".
Have a webdriver wait condition like waiting for an element to be clickable and then use your above code.
Thanks for you help all. The element wasn't being found because it was in an iframe popup and Selenium was searching for it in the page behind.
This post: https://stackoverflow.com/a/32836709/6565982 helped.
For anyone in the future my code is now:
WebElement iFrame= driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iFrame);
// Select an address
driver.findElement(By.cssSelector("span[data-seleniumid=\"Address0\"]")).click();
// Switch back to the default page
driver.switchTo().defaultContent();
Thanks again.

Categories

Resources