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)))
Related
I am a beginner in selenium and I would like to press a file submission field.
I have already done a whole code to connect to the page, click on the buttons etc. (everything works, my driver is good)
But impossible to click on adding file
I looked on the internet how to do it, I added time, tried to browse the frames, used javascript for the hidden class... I tried all the buttons in the field and it doesn't detect them.
Add File
Source code
Thread.sleep(2000);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
WebDriverWait wait = new WebDriverWait(driver, 60);// 1 minute
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*#id=\"yui_3_17_2_1_1584634673387_348\"]/div[1]/div[1]/a")));`
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="yui_3_17_2_1_1584634673387_348"]/div[1]/div[1]/a"}
Do you have an idea ?
The primary issue I observed looking at the code is the incorrect locator in your Explicit Condition element checking line. It can be replaced with below code:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='yui_3_17_2_1_1584634673387_348']/div[1]/div[1]/a")));
Basic XPath Syntax for reference though it has it's variations:
//tagname[#attrbute='value']
Additional advice Though I am not sure about the application you are automating, but the ID is likely to be changed. Based on the DOM Structure you have provided in the link above I would say change the locator to something on the lines of:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#role='button'][#title='Add..']")));
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.
click here to check the page I am working on
The following is the java code:
driver.findElement(By.xpath(".//*[#id='ref_2665398031']/li[4]/a/span1")).click();
The element is actually in the left navigation pane.
Here when this particular statement is getting executed, I can see that browser is moving down, but it does not click that element.
You have not properly written the xpath:
".//*[#id='ref_2665398031']/li[4]/a/span1"
it should be : "//[#id='ref_2665398031']/li[4]/a/span[1]"
and if using WebDriver then
"//[#id=\"ref_2665398031\"]/li[4]/a/span[1]"
After correcting above if still error exists:
You are using a relative path which is relative to the element having id='ref_2665398031.
This id seems to be generated dynamically.
Please confirm two things:
Your code is not giving element not found error.
Page refresh is not changing this id. (If it is being generated dynamically than it will change on page refresh.)
Otherwise try using different approach:
driver.findElement(By.xpath("//*[contains(text(), '50% Off or more')]"));
Use this xpath:
driver.findElement(By.xpath("//ul[#id='ref_2665398031']/li[4]/a/span[1]")).click();
Try Below xpath it is working for me
driver.findElement(By.xpath("//ul[contains(#id,'ref_2665398031')]/li[4]/a")).click();
Try JavaScriptExecutor instead of using normal Selenium click method.
WebElement element = driver.findElement(By.xpath(".//span[contains(text(),'50% Off or more')]"));
((JavascriptExecutor) driver).executeScript("return arguments[0].click();", element);
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 :)
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.