I want to be able to click a logout button twice very rapidly like a user would?
WebElement logout = driver.findElement(By.id("dijit_form_Button_0_label"));{
if(logout.isDisplayed()){
logout.click();
I want to be able to click on the logout button twice at less than a second like a user would? Is this possible in selenium webdriver?
EDIT:
Make sure the dom is loaded
Like atri said, you could use the double click function according to This thread.
WebElement logout = driver.findElement(By.id("dijit_form_Button_0_label"));{
if(logout.isDisplayed()){
logout.doubleClick();
if you don't want to use the doubleClick function, I would recommend the ExplicitWait from Selemium
Selenium: Implicit and explicit Wait
If you want to do this manually, could add a delay between your click using javascript Thread and selenium wait.
Based on This thread
WebElement logout = driver.findElement(By.id("dijit_form_Button_0_label"));{
if(logout.isDisplayed()){
logout.click();
Thread.sleep(100);
logout.click();
}
Better way is to use ExplicitWaits which you means you will wait
exactly as long as some action happens or some element gets rendered
on the page.
- petr-mensik
An explicit waits is code you define to wait for a certain condition
to occur before proceeding further in the code. The worst case of this
is Thread.sleep(), which sets the condition to an exact time period to
wait. - Selenium
Related
Yesterday I ask question how to deal with: "Element is not clickable at point - other element would receive the click" exception in Selenium WebDriver.
I receive several answers with this solution:
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(...));
Yes, it has sense but in practice it doesn't work.
Below is piece of my code:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", categoryItem);
Thread.sleep(1000);
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(categoryItem));
categoryItem.click();
List<WebElement> selects = driver.findElements(By.tagName("select"));
Select ticketsSelect = new Select(selects.get(3));
ticketsSelect.selectByValue("2");
By addToBasket = By.xpath("//div[contains(text(),'Add to Basket')]");
Thread.sleep(1000);
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(addToBasket));
driver.findElement(addToBasket).click();
I had to put 1 second sleep before each wait until element to be clickable because without this it still throw exception that other element would receive the click.
My question is why WebDriver clicks this element if element is not clickable ?
ElementToBeClickable – An expectation for checking an element is visible and enabled such that you can click it.
But it seems that it doesn't imply that this element is not "hidden" under some other element, even if it's a transparent part of another tag. In my experience, this exception is mostly occur when you are trying to interact with a page, which currently loads, and you have typical loading div overlapping the whole page with loading icon, or in other cases when you have an open modal, which also often cover page with overlays transparent overlay, to prevent user from interacting with a page.
In this answer you can see that you basically have two options:
You can send a click event via JavaScript call. It will always work, but it's not something you want to do. If you want to test your page, you should always trigger real click events.
Wait. There is not much you can do. Something you just need to wait till your page fully loads. In a way maybe this will show you the slow parts of your application, which you can improve in a future
I'm using java with selenium IEDriverServer. I would like to know if is possible to click on a pop up on a page which has not completed loading. When I get the pop up, my code is not running until I click on Ok on pop up.
Thanks.
It's not possible to click on element that is not visible.
But you can add the following config, it will tell the driver to implicitly wait for elements:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
As you mentioned if is possible to click on a pop up I presume it's a JavaScript Popup.
Essentially, until & unless the synchronous/asynchronous JavaScript/AJAX Calls gets executed completely the Page Loading won't be complete. Hence, invoking click() method while Page Loading may not fetch optimum results.
Execution process difference between below two statements.
driver.findElement(By.xpath("//input[#value='Save']")).click();
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
WebDriver click() simulates real user interaction with the UI. I will be performed (in most browsers) by sending a native event to the browser, and it has to be visible in order to click on it. From the docs
...if click() is done by sending a native event (which is the
default on most browsers/platforms)
There are some preconditions for an element to be clicked. The element
must be visible and it must have a height and width greater then 0.
JavaScript click() on the other hand
Executes JavaScript in the context of the currently selected frame or
window.
Regardless if the WebElement is visible or not. This approach misses the idea of user interaction Selenium tries to simulate.
in simple terms. Webdriver uses a native browser events to click on element, and javascript uses JavaScrip to click on the element.
If I remember correctly Selenium 1, was using JavaScript for all it's action but they changed this in webdriver (Selenium 2) and now they are using native browser events to interact with browser. And for this reason you required corresponding support from browser (geckodriver, IEDriver, Chromedriver, etc..). JavaScript engine on the other hand is inbuilt in all the major browsers and so you don't require this extra executables.
I have a method that clicks on a button, however, when it is run, selenium returns the outcome as clicked successfully, when, in reality, the button is not actually clicked. If I run the test several times, occasionally, it'll be clicked as expected. I have my test framework set as an implicit wait for about 15 seconds, I have set an explicit wait for this element, and still see the same issue. When I do <element>.isDisplayed(), the element is always found. I placed the .click in a while loop to click it a few times which works most of the time, however, still sometimes the test fails. Is it possible to have an if statement to check if an element is actually displayed before clicking the button?
I've tried:
if(!element.isDisplayed){
element.click
}
Here is the button I am having issues with:
<button class="notkoButton listNew">
<div class="l6e iconAdd">New List</div>
</button>
Here is my method:
public marketing_lists_page navigateToNewListPage() throws Throwable {
try {
int x = 0;
while(x < 5) {
newListBtn.click();
x++;
}
//newListPageHeader.isDisplayed();
} catch (NoSuchElementException e){
logs.errorDetails("Could not navigate to New List Page");
Assert.fail();
}
return this;
}
Seems like the element is not enabled or not clickable initially. And to answer your question, yes there is an explicit wait you can use and wait for the element to be clickable:
WebDriverWait wait = new WebDriverWait(driver, timeOut);
wait.until(ExpectedConditions.elementToBeClickable(locator));
Try this, Click using javascript and feel free to change the locate for the element according to your convenience:-
WebElement element= driver.findElement(By.xpath("YOUR XPATH"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Hope it will help you :)
Try scrolling to the element before clicking on it. This mostly happens when you test on chrome. You can use JavaScriptExecutor to scroll.
Something like this:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0," + element.getLocation().Y + ")");
You say you have tried an explicit wait, but it sounds like what you are asking for can be best accomplished by a fluentWait (which is type of explicit wait):
public WebElement fluentWait(final By locator) {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
});
return foo; };;
Here the WebElement would be the button you are trying to click. The cool thing about FluentWait is that the element itself is returned if it is found. From the documentation:
An implementation of the Wait interface that may have its timeout and polling interval configured on the fly. Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.
To use it simply do:
WebElement newListBtn = fluentWait(By.id("button"));
Also try:
Driver.SwitchTo().DefaultContent();
before clicking just in case it is a frame issue.
Selenium suffers from lack of GUI "dynamism".
By that i advice you to send a refresh to the page after your click.
Tell me what's up.
I was facing the exact same issue but only after I updated my selenium libraries from 2.45 to 2.48.2. "Click" method call never fails which mean the element is always found by the driver, what I think it fails to do is clicking the right spot may be. My test would run fine on a higher res screen and would sometimes pass (but mostly fail due to click issues) on a lower res screen. I tried many things but in the end what worked for me was ZOOM.
Before running the test I call the following method and the "click" method works as expected on that low res screen now.
public void zoomOut () {
WebElement html = driver.findElement(By.tagName("html"));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
}
I had what looked like a similar problem on the surface of it while using the JavaScript WebDriver - the click() promise resolved successfully, and yet nothing happened.
In my case, it turned out to be related to an invalid image src attribute on the page (unrelated to the button I was clicking). For whatever reason, fixing the image link solved the problem!
I know the OP was using Java rather than JS, but my own searches brought me here, so others using JavaScript will likely come the same way.
I've noticed clicks being inconsistent with Selenium as well, this seems to be a known issue to a certain extent:
https://github.com/SeleniumHQ/selenium/issues/4075
In my case there is practically no way for the element to not be ready. I had a wait, but more importantly my test case was filling in a very large form, the button that was to be clicked to initiate a drop-down is very simple and I can see it being loaded close to half a minute before my test interacts with it, yet every so often it would still fail. To the point where I wrote a silly while loop to click every 100 millisecond until the expected dropdown showed up.
This is all guessing but from personal observance and some comments from others in that thread - such as one user mentioning they notice more issues like this when running parallel tests, seem to indicate it is possible for what seems to be a missed event, and perhaps this is due to the lag introduced from it's architecture. Specifically I'm comparing this to how Cypress works with native commands given to the browser directly without the intermediary server.
I have a scenario where I am not able to proceed further:
I have two buttons on the page (both the buttons are in the same frame). I'm using an Iterator for the Button 1. When we click the Button 1, it makes an AJAX call. I see a mouse loading animation for a second and then the product is added to the cart.
Say, if I have 5 buttons of the same type & want to click all the 5 buttons, when I use the iterator to click the 5 buttons, the control is going out of the loop instead of clicking the second button.
Iterator<WebElement> button1 = T2.iterator();
while(button1.hasNext()) {
button1.next().click();
Thread.sleep(10000);
}
driver.findElement(By.id("button2 id")).click();
If I use the Thread.sleep(), it's working fine. But I don't want to use that as its not the proper way.
The button2 id is also in enabled state, I wasn't able to use the Wait for the element to be enabled. When the execution comes to the button1.next().click(), i,mediately it's going to the next line, without completing the loop. If i print some text after the button1.next().click(), the text gets printed 5 times. But don't know why the button is not clicked.
Even tried implicit wait, but no luck.
What is the best way to avoid such kind of issue?
The proper way is to wait explicitly for the product to be added to the cart. Something changes on the page after the operation finishes, right? So wait for that change to occur! I don't know your webpage, obviously, but something along the lines:
// earlier
import static org.openqa.selenium.support.ui.ExpectedConditions.*;
// also earlier, if you want, but you can move it to the loop
WebDriverWait wait = new WebDriverWait(driver, 10);
Iterator<WebElement> button = T2.iterator();
while(button.hasNext()) {
// find the current number of products in the cart
String numberOfProductsInCart = driver.findElement(By.id("cartItems")).getText();
button.next().click();
// wait for the number of items in cart to change
wait.until(not(textToBePresentInElement(By.id("cartItems"), numberOfProductsInCart)));
}