Selenium Click with actions vs click without actions - java

I was testing a scraper and I noticed that when using:
WebElement tab = driver.findElement(By.cssSelector("div[id*='tabOne']"));
tab.click();
I receive this message constantly:
1536868112230 Marionette DEBUG [6442450945] Canceled page load listener because no navigation has been detected
However when I use this instead:
Actions builder = new Actions(driver);
WebElement tab = driver.findElement(By.cssSelector("div[id*='tabOne']"));
builder.click(tab).perform();
I don't receive a message every time I sift through the tabs or click on new links.
Am I receiving this message because I am not emulating the user behavior thus I get thrown this message because it was waiting for a user click on the tab? And since I am scraping and not testing, is it better to just use the former instead of the latter since I don't need to interact with the GUI anyways?

Related

Does Selenium WebElement.Click() wait until the next page is loaded?

I tell Selenium to wait until it sees an element - Selenium sees it
I tell Selenium to click on this element, it is a button to link to a new page - Selenium click on it.
The problem is that after clicking it, Selenium will then wait until the next page is fully loaded (the page sometimes loads in a second, or waits for ages, I think it's a problem with Zen Desk Live Chat on that page).
When it is fully loaded it will then throw an error and say that the element it clicked on cannot be seen (naturally it can't because it is now on a new page)
I have tried changing the wait time with
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
however this doesn't help.
I have also tried
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath(element)));
However this has the same problem.
Below is the code I am currently using.
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath(element)));
WebElement we = driver.findElement(By.xpath(element));
we.click();
System.out.println("Clicked: " + element);
}catch (Exception e){
System.out.println(e.getMessage());
}
I expect that once the element has been clicked on, that Selenium just carries on without caring if the next page has loaded up or not.
However what happens is when the 2nd page loads up, sometimes the page gets stuck "waiting for widget-mediator.zopim.com" and Selenium will not progress past the click() line until the the WebDriverWait time has expired (60 seconds)
After the 60 seconds has expired I see this error in the Console Output:
[1561374309.111][SEVERE]: Timed out receiving message from renderer: 60.000
[1561374309.112][SEVERE]: Timed out receiving message from renderer: -0.002
Is something else happening here? Or does Click() wait until the page has loaded, if that click takes it to a new page? If it does is there a way to tell it to stop waiting? I have other code to check if the page has loaded or not, but I don't need Click() to do this.
Thanks in advance.
Selenium’s (or more correctly, WebDriver’s) behavior on click is governed by the W3C WebDriver Specification. In that document, the full algorithm is defined for what happens when an element click is requested. In general, if the click will navigate to a new page, the driver will wait for that new page to be “loaded” (scare quotes intentional) according to the page load strategy until the page load timeout.
The page load strategy defaults to “normal”, or waiting for the document’s readyState to be complete. If you set it to “none” in the capabilities requested during driver instantiation, the driver will not wait at all. Choosing that route would mean you would need to handle all synchronization for pages being loaded. Note there is a third page load strategy, “eager”, but at the time of this writing, not all WebDriver implementations (most notably chromedriver) support it.
You can adjust the page load timeout at runtime in your Selenium code. One approach might be to lower the timeout to a relatively low value for the duration of clicking on this particular element, then restoring it to its prior value afterward. The drawback here is that you will have to catch the timeout exception that is thrown when the page load times out before continuing.

On which Thread JxBrowser execute click DOM element

Here we have a simple DOMElement
DOMElement button = document.findElement(By.id("action_button");
... and when I execute click on them as
button.click();
And I have a question: Did API will execute click on the same Thread ? Or will be created other Thread which execute this .click() ?
I ask about it because I search best way (the fastest way) to execute click at DOM elements by browser.
When you invoke the click() method it adds the click message into the message queue which is processed in the separate thread asynchronously by message sender.

Notification(Alert) comes randomly throughout application in selenium webdriver

I am using selenium webdriver with Java language. I am facing an issue that notification alert appears randomly through out application. Basically these alert have some information as it is functionality .
As script performs its steps but suddenly these alert appears on the screen and my script would fail due to alert message.
Please give your suggestions, How can we handle this type of alert which appears randomly on any window?
Below are two points come in my mind to handle this scenario:
I will check Alert appears or not at every step (after click or
other action) but it increases my execution time.
Is there any way, we keep monitoring that Alert appears or not. If
Alert appears just close the Alert if not then keep execute the
steps of scripts.
Please suggest any workaround to handle such Alert so that our scripts do not fail.
This is the same scenario that we automate mobile application using Appium tool and suddenly any advertisement come on the screen.
It would be good if any one provide java code to handle this type of scenario.
Thanks in advance!!
If you want that unnecessary alert not appear during your script execution you can override you alert function before executing your script by using JavaScriptExecutor as below :-
JavascriptExecutor executor = (JavascriptExecutor)driver
executor.executeScript("window.alert = function () { return true}");
You can execute this script once every time when your page is loaded. this script will override your alert functionality and the alert will never occur.
I suggest you this script run when if your test is not depend on alert because after execute this script the alert will not be appear on the page.
other than you can handle alert as below :-
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
Now if you want to accept alert, you can use :-
alert.accept();
and for cancel, you can use :-
alert.dismiss();
Note :- if alert is not present by using WebDriverWait it will throw TimeoutException.. so you need to handle it.
Edited..
For Appium automation to solve this problem, you can use a desired capability specifically designed to handle these alerts.
You can either always accept or always dismiss the alerts with these desired capabilities :-
autoAcceptAlerts = true
...
capabilities.SetCapability("autoAcceptAlerts", true);
or
autoDismissAlerts = true
...
capabilities.SetCapability("autoDismissAlerts", true);
Furthermore, some of the older versions of Appium haven’t worked with this solution, so you might want to try a small workaround with this :-
driver.SwitchTo().Alert().Accept();
For more info follow this
Hope it will help you..:)
You can call this method wherever you are getting the alert.This method will accept the alert.
public void checkAlert(){
if(ExpectedConditions.alertIsPresent() != null){
driver.switchTo().alert().accept();
}
}

Selenium Webdriver - Chrome - Switch Window and back - Unable to receive message from renderer

This is my first question on Stack Overflow. Thanks to all StackOverflow users who keeps technology passion ticking.
I am testing a web application with selenium Webdriver . It is payment webpage where, after selecting Payment method as 'PayPal' it opens up a new Popup , a PayPal popup and i Switch window to Paypal , do all my necessary Transaction. And once the Transaction is successful , automatically the paypal popup is closed, and i am not able to return to my original window from where i have initiated transaction.
I am getting following error in eclipse console:
Starting ChromeDriver (v2.9.248315) on port 25947
[70.164][SEVERE]: Unable to receive message from renderer
The following details might help :
selenium Webdriver (2.28.0)
java - JRE7
Google Chrome Version - Version 33.0.1750.146
Test Framework - Test NG
Here is my code :
// To Switch to Popup/Paypal window
String currentWindowHandle=driver.getWindowHandle();
Set<String> openWindowsList=driver.getWindowHandles();
String popUpWindowHandle=null;
for(String windowHandle:openWindowsList)
{
if (!windowHandle.equals(currentWindowHandle))
popUpWindowHandle=windowHandle;
}
driver.switchTo().window(popUpWindowHandle);
// Carraying out my paypal transaction
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[#id='loadLogin']")).click();
Thread.sleep(8000);
WebElement login_email = driver.findElement(By.xpath("//*[#id='login_email']"));
login_email.clear();
login_email.sendKeys(Keys.BACK_SPACE);
login_email.sendKeys("abc#abc.com");
WebElement login_password = driver.findElement(By.xpath("//*[#id='login_password']"));
login_password.clear();
login_password.sendKeys("abcxyz");
// Next Click is Final Click on PayPal
driver.findElement(By.xpath("//*[#id='submitLogin']")).click();
// Transaction is finished on PayPal side and it automatically popup is closed
//Now i am trying to switch to my last working(original) window
driver.switchTo().window("My Web Page Title");
You should be using:
driver.switchTo().window(currentWindowHandle);
It causes because page took long time to load , you need add additional line to your chromedriver option.
System.setProperty("webdriver.chrome.driver","E:\\selenium\\chromedriver_2.41\\chromedriver.exe");
//mention the below chrome option to solve timeout exception issue
ChromeOptions options = new ChromeOptions();
options.setPageLoadStrategy(PageLoadStrategy.NONE);
// Instantiate the chrome driver
driver = new ChromeDriver(options);
The problem is resolved.
The place where I was declaring currentWindowHandle was after clicking and it takes the new window as the Current Window handle.
I just moved below statement to before the new window click event.
String currentWindowHandle=driver.getWindowHandle();
Thanks all for your time and Help.

MessageBox with timeout in Selenium

After writing some automated tests with selenium, I want a message box
telling the testers which test is launched.
It is important, that the test must not run when the messagebox is shown.
They should be paused when the messagebox is shown.
I did this by JavaScript
Object result = ((JavascriptExecutor)TestRunner.driver).executeScript("alert('" + text + "');");
Now I want this messagebox to be shown some seconds. I tried:
TestRunner.driver.manage().wait(10);
And
Selenium selenium = new WebDriverBackedSelenium(TestRunner.driver, TestRunner.driver.getCurrentUrl());
selenium.start();
selenium.getConfirmation();
And
WebDriverWait waitForOkay = new WebDriverWait(TestRunner.driver, 10);
waitForOkay.wait(10);
But the alert always immediately disapears, just like there is some
alert.accept();
Is there any way to have a messagebox that is either clicked "Ok" or
some timeout (e.g. 10 seconds) to not block automated tests?
Any input (like other ways to achieve that) very welcome!
A wait condition is unnecessary in this situation, since by Default the JavaScript alert box.... blocks the execution till you click on "Ok" button.
I tried with sample code (C# snippet) and it was working.
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.google.com");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(4));
Console.WriteLine("Entering the Sync Call");
js.ExecuteScript("alert('"+ Error+"');");
Console.WriteLine("Exiting the Sync Call");
I hope this helps...All the Best :-)

Categories