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()']
Related
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']
I want to click a button(to send a form)
<button class="form-button primary">Click here</button>
I find the element like this:
driver.findElement(By.xpath("//button[contains(text(),'Click here')]")).click;
On chorme is working and sending the form but in IE11 is not(sending the form).
To be clear, in IE is finding the element(or an element). But probably is not the correct element.
Addition info:
This is the only button with this text
I can probably find other ways to get this element , but if I rework this path I will need to change all the paths similar to this.
Selenium version:3.14
IE webdriver :3.14
There are several different ways of clicking on something using selenium. I would try using either a javascript click or an action click.
Javascript click:
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Action click:
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
action.moveToElement(element).click().build().perform();
It's also possible that you are in the wrong frame while you are executing the click.
driver.switchTo.frame("Frame_ID");
You would be able to find the frame ID when you inspect the webpage.
If your usecase is to invoke click() you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
xpath using className and text:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='form-button primary' and text()='Click here']"))).click();
xpath using text:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Click here']"))).click();
xpath using contains():
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(., 'Click here')]"))).click();
I'm trying to select the highlighted element below (which is a "window close" button):
There is one other element with class='icon-Dismiss' on the page, but none with class='dialog-close'.
What I have tried so far:
driver.findElement(By.xpath("//*[#class='icon-Dismiss' and #class='dialog-close']"))
driver.findElement(By.className("dialog-close"))
driver.findElement(By.xpath("//*[#id='contentBox']"))
In all cases however, I receive the following error:
no such element: Unable to locate element
Does anyone have an idea on how I can select this element?
You need to induce WebDriverWait for the desired elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.contentBox div.icon-Dismiss.dialog-close"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='contentBox']//div[#class='icon-Dismiss dialog-close']"))).click();
Try using
driver.findElement(By.cssSelector(".icon-Dismiss.dialog-close"))
Also, make sure this element is not in an iFrame. If it is then you will need to switch to the iFrame first before you can find the element.
try this driver.switchTo().alert().dismiss(); if that dialog box is an alert.
If you just want to locate then
driver.findElement(By.cssSelector("div.icon-Dismiss.dialog-close"));
but if you want to just close the alert then alert().dismiss(); will be the best option.
If this is the window pop up
try : driver.switchTo().alert().dismiss();
if this is application these locators should work
Note : make sure you are using waiting till popup appearing on page
I am coding a selenium webdriver test in Java. I need the test to click on a "Yes" button in chrome that does not have an id or name.
I cannot find the element to click on the button that only has "Yes" as its unique identifier. There is also a "No" button.
I have tried to find the WebElement using xpath, classname and I have tried findElements function. Nothing succeeds.
This is the HTML:
<span class="ui-btn-inner">
<span class="ui-btn-text popup-anchor-text">Yes</span>
</span>
I have tried:
WebElement yesBtn = browser.findElement(By.xpath("//div[#class='ui-btn-text popup-anchor-text']/span"));
WebElement yesBtn = browser.findElement(By.xpath("//span[.='Yes']"));
WebElement yesBtn = browser.findElement(By.xpath("//div[contains(text(), 'Yes')]"));
WebElement yesBtn = browser.findElement(By.xpath("//button[#class='ui-btn-text popup-anchor-text' and span='Yes']"));
WebElement yesBtn = browser.findElement(By.xpath("//div[#class='ui-btn-text popup-anchor-text' and span='Yes']"));
yesBtn.click();
List<WebElement> yesBtn = browser.findElements(By.className("ui-btn ui-shadow ui-btn-corner-all ui-btn-up-a"));
yesBtn.get(0).click();
Error message:
NoSuchElementException; no such element. Unable to locate element.
The correct XPath locator would be:
//span[text()='Yes']
Just in case you can go for normalize-space() function:
//span[normalize-space()='Yes']
If this doesn't help:
Make sure that the span doesn't belong to an iframe, otherwise you will have to switch to the iframe before attempting to locate the element.
driver.switchTo().frame("your-frame");
Make sure to use WebDriverWait class just in case the element is not immediately available so WebDriver would perform several find attempts with polling interval
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']")));
Try out //span[#class='ui-btn-inner']/descendant::span[contains(text(), 'Yes')]. It should help out.
<button class="md-trigger btn btn-primary mrg-b-lg" data-toggle="modal" data-target="CaseProcessmodal" id="AddCaseButton" onclick="return validateForm('#CaseProcessmodal');">Add Case</button>
I have an element location like above, trying to click on the button in selenium, its not working. I am using page factory, element id is correctly matching though.
Verified with other matching element id.
The desired element is within a Modal Dialog so to click() on the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.md-trigger.btn.btn-primary.mrg-b-lg#AddCaseButton[data-target='CaseProcessmodal']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='md-trigger btn btn-primary mrg-b-lg' and #id='AddCaseButton'][text()='Add Case']"))).click();
I found solution to this putting if condition, agian applied click action. it worked.