I am trying to automate functional testing of a web application using Selenium webdriver and Java.
In the AUT, there is a 'Submit' button defined by the following html code
<button id="submitbtn" class="btn btn-primary" type="submit">Submit</button>
I use the following command to click the button.
driver.findElement(By.id("submitbtn")).click();
When I run the code, the webdriver can find the button but the click action is not performed (I can understand that webdriver can find the button because no exception is thrown and the I can see a selection on the button when the code is run). I tried different waits
new WebDriverWait(driver,60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("submitbtn"));
but not getting any positive result. If I use,
Thread.sleep(3000);
it works fine (but I want to avoid this code). I tried all other types of waits and action class,
Actions action=new Actions(driver);
action.moveToElement(driver.findElement(By.id("submitbtn"));
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("submitbtn")));
action.click().perform();
but no luck. Is there any way to achieve this?
How about JavascriptExecutor?
WebElement element = driver.findElement(By.id("submitbtn"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
A submit() is an option driver.findElement(By.id("submitbtn")).submit();. More information here
Related
Unable to click on webelement button
I tried to click on button by mouse movement but no success
my outer html is as below :
<button class="btn btn-alt btn-small" type="button" ng-click="ecdapp.uploadBlueprintModalPopup();">
Create
</button>
button xpath is:
//*[#id="page-content"]/div[3]/button
Not seeing the full page source it's hard to tell where your XPath expression is good or not, you can try locating the button using its text instead
//button[normalize-space(text())='Create']
the normalize-space() function is used to discard heading/trailing whitespaces
It might also be the case the button is not immediately available, I would recommend considering using Explicit Wait approach via WebDriverWait class
WebElement myButton = new WebDriverWait(driver, 10)
.until(ExpectedConditions
.elementToBeClickable(By.xpath("//button[normalize-space(text())='Create']")));
myButton.click();
the above code will try to locate the aforementioned button for 10 seconds and click it as soon as it will be present/visible/clickable. Otherwise it will fail with NoSuchElementException
May be the Xpath is wrong. Try the below xpath:
//button[contains(text(),'Create')]
As you can see on the screenshot this Xpath 100% works, if you still won't be able to click on that button, then problem is not with xpath. Let me know if its still fails.
By.xpath("//button[#class = 'btn btn-alt btn-small' and #type = 'button']")
Based on your comment:
I tried this code , but unable to click . element click intercepted: Element ... is not clickable at point (293, 97). Other element would receive the click: ... (Session info: chrome=74.0.3729.169)
I pretty sure I know whats your problem, before u click on this element, something going on the page:
It says - Other element would receive the click, means there is other element above(overlapping) your button(pop up window, page is greyed out(disabled while loading, Some JS running)), so when Selenium trying to click on your button its actually clicking on that blocking element.
Try to click after Thread.Sleep(); wait 5-10 sec.
If this is the case then u need to add condition before find your button to check that element that prevent from clicking on button is disappeared then u click on it.
Try JavaScript executors as below,
WebElement element = driver.findElement(By.xpath("<xpath of button>"));
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
I am not able to click on the submit button inside the modal. It only works sometimes - it's not stable.
Below is my HTML element:
<button id="submit-btn" name="submit" data-dismiss="modal" type="submit" class="btn btn-info btn-sm submit projectSaveBtn">Submit</button>
Here I'm using id for locating the element, but I'm not able to click on the submit button inside the modal.
My Java code:
WebElement element1 = driver.findElement(By.id("submit-btn"));
Actions actions = new Actions(driver);
actions.moveToElement(element1).build().perform();
wait.until(ExpectedConditions.elementToBeClickable(element1)).click();
When we use the action class, selenium takes mouse and keyboard controls. If we interact with mouse or keyboard at the time of test execution (specially the actions statement execution), it may fail some times.
You can try with actions class and don't interact until it completes the test execution. it may solve your issue.
Instead of using the id attribute you can use an xpath as follows :
WebElement element1 = driver.findElement(By.xpath("//button[#id='submit-btn']"));
element1.click();
or
WebElement element2 = driver.findElement(By.xpath("//button[#class='btn btn-info btn-sm submit projectSaveBtn' and #id='submit-btn']"));
element2.click();
If it's a windows modal dialog box, you might want to first switchTo() to the modal and then perform accept() on it.
driver.switchTo().alert().accept();
driver.switchTo().window("");
I solved the issue, it was timing issue .Modal was opened before the page loaded properly so giving the time before clicking/opening modal solves the issue .
Here is my code -->
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement add = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.projectAddBtn")));
add.click();
This solved my issue.Thank you all for the support :-)
I am getting the following error when I attempt to click a button:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 10.06 seconds
I've tried the following and none worked:
1) Waiting for 10 seconds in case the page is being loaded
2) Used JS executor thus:
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", By.cssSelector("#IconButton > input.IconButtonDisplay"));
3) Used wait until element is visible
Number 2 is actually executed but the results of the click don't eventuate i.e. new page does not open.
Number 3 times out stating button is not visible, but button is visible and can be manually clicked.
What I can tell you is that using the Selenium IDE I am able to playback and click the button no problems.
HTML of button (can't put too much here as proprietary information). Apologies for formatting:
<div widgetid="dijit__WidgetsInTemplateMixin_13" id="dijit__WidgetsInTemplateMixin_13" class="gridxCellWidget">
<div class="IconButton" widgetid="IconButton" id="IconButton" data-dojo-type="ab.cd.ef.gh.IconButton" data-dojo-attach-point="rowBtn1Pt">
<input class="IconButtonDisplay" src="/tswApp/ab/cd/ef/gh/images/edit.png" style="width: 20px;" type="image">
</div>
</div>
In Javascript executor you want to pass the instance of WebElement not the By selector. So change
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", By.cssSelector("#IconButton > input.IconButtonDisplay"));
to
WebElement element = driver.findElement(By.cssSelector("#IconButton > input.IconButtonDisplay"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Have you tried just using FirefoxDriver?
FirefoxDriver driver = new FirefoxDriver();
Have you tried just using this? Is this not unique enough?
driver.findElement(By.cssSelector("input.IconButtonDisplay")).click();
If not, try this (it's the equivalent to what you were doing with JSE)
driver.findElement(By.cssSelector("#IconButton > input.IconButtonDisplay")).click();
Maybe it's not the INPUT that takes the click? Have you tried clicking either of the parent DIVs?
driver.findElement(By.id("IconButton")).click();
or
driver.findElement(By.id("dijit__WidgetsInTemplateMixin_13")).click();
for me this is what worked:
JavascriptLibrary jsLib = new JavascriptLibrary();
jsLib.callEmbeddedSelenium(driver,"triggerMouseEventAt", editButton,"click", "0,0");
Hope this helps others who are having the same problem.
I want to click the load More link in a page. My code is below.
pageUrl="http://www.foundpix.com/category/actor/bollywood-actor/"
WebDriver driver = new FirefoxDriver();
driver.get(pageUrl);
driver.manage().window().maximize();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,2500)", "");
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("json_click_handler")));
driver.findElement(By.id("json_click_handler")).click();
How can I make it click the link.
You can use below xpath to click Load More button both the times:-
driver.findElement(By.xpath("//*[#id='blocks-left']/div/div[3]/div[contains(.,'Load More')]")).click();
this button change the location after you click on it and it can be clicked twice so:
before the first click use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div")).click();
after first click,you can use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div[2]")).click();
Maybe come at it from a different angle. Do you really need to have the link clicked or do you have some Javascript function that gets called on click of the link (like window.loadMore). Can you call the function directly? Selenium is a bit annoying in the sense you can only click a visible element (I don't mean it has to be in the viewport - it just can't have a style like display:none;).
In testing a simple website, I find that when using the Firefox webdriver I am unable to get a Javascript calendar window to appear when the button is clicked. I am able to spawn the window in the Selenium IDE but when I run the Java code, the click is registered, but no window is spawned.
The Code I am using to click the Javascript element is:
WebElement element = driver.findElement(By.cssSelector("img[alt=\"Pick a date\"]"));
element.click();
Additional info: The 'cal.gif' image is also not shown when using the webdriver. The problem is NOT switching to the calendar window or selecting and element within, it is simply getting the window to spawn at all.
This is the website under test: Parking Meter
I have searched quite a bit for the solution, either I am not searcher the right keywords or I am missing something obvious, any help would be appreciated.
edit: HTML code for the JS calendar:
<a href="javascript:NewCal('EntryDate','mmddyyyy',false,24)"
<img height="16" width="16" border="0" alt="Pick a date" src="cal.gif"></img>
</a>
What your doing here is wrong. You need to click on the a tag not on the img tag.
Look at the below code, which is working fine for me:
#Test
public void testSO() throws Exception
{
driver.get("http://adam.goucher.ca/parkcalc/index.php");
Thread.sleep(2000);
driver.findElements(By.tagName("a")).get(0).click();
}
Change the index to 0 or 1 accordingly.
Id be using the JavascriptExecutor.
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("<JavaScript.click;");