Page load strategy for Chrome driver (Updated till Selenium v3.12.0) - selenium

I'm using Chrome browser for testing WebApp.
Sometimes pages loaded after very long time. I needed to stop downloading or limit their download time.
In FireFox I know about PAGE_LOAD_STRATEGY = "eager".
Is there something similar for chrome?
P.S.: driver.manage().timeouts().pageLoadTimeout() works, but after that any treatment to Webdriver throws TimeOutException.
I need to get the current url of the page after stopping its boot.

ChromeDriver 77.0 (which supports Chrome version 77) now supports eager as pageLoadStrategy.
Resolved issue 1902: Support eager page load strategy [Pri-2]
From the Webdriver specs:
For commands that cause a new document to load, the point at which the command returns is determined by the session’s page loading strategy.
When Page Loading takes too much time and you need to stop downloading additional subresources (images, css, js etc) you can change the pageLoadStrategy through the webdriver.
As of this writing, pageLoadStrategy supports the following values :
normal
This stategy causes Selenium to wait for the full page loading (html content and subresources downloaded and parsed).
eager
This stategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).
none
This strategy causes Selenium to return immediately after the initial page content is fully received (html content downloaded).
By default, when Selenium loads a page, it follows the normal pageLoadStrategy.
Here is the code block to configure pageLoadStrategy() through both an instance of DesiredCapabilities Class and ChromeOptions Class as follows : :
Using DesiredCapabilities Class :
package demo; //replace by your own package name
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class A_Chrome_DCap_Options {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
DesiredCapabilities dcap = new DesiredCapabilities();
dcap.setCapability("pageLoadStrategy", "normal");
ChromeOptions opt = new ChromeOptions();
opt.merge(dcap);
WebDriver driver = new ChromeDriver(opt);
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
driver.quit();
}
}
Using ChromeOptions Class :
package demo; //replace by your own package name
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class A_Chrome_Options_test {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions opt = new ChromeOptions();
opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);
WebDriver driver = new ChromeDriver(opt);
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
driver.quit();
}
}
Note : pageLoadStrategy values normal, eager and none is a requirement as per WebDriver W3C Editor's Draft but pageLoadStrategy value as eager is still a WIP (Work In Progress) within ChromeDriver implementation. You can find a detailed discussion in “Eager” Page Load Strategy workaround for Chromedriver Selenium in Python
References:
WebDriver navigation
WebDriver page load strategies
WhatWG Document readyStateChange / readiness

For Selenium 4 and Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.page_load_strategy = 'none'
driver = webdriver.Chrome(options=options)
driver.get("http://www.google.com")
driver.quit()
For more details can be found here https://www.selenium.dev/documentation/webdriver/capabilities/shared/#none

In C#, since PageLoadStrategy.Eager doesn't seem to work for Chrome, I just wrote it myself with a WebDriverWait. Set the PageLoadStrategy to none and then doing this will basically override it:
new WebDriverWait(_driver, TimeSpan.FromSeconds(20))
.Until(d =>
{
var result = ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState");
return result.Equals("interactive") || result.Equals("complete");
});
You just add in your chrome driver as a parameter and the TimeSpan is set to a max of 20 seconds in my case. So it will wait a max of 20 seconds for the page to be interactive or complete

Try using explicit wait . Visit this link. It might be helpful
Try this code as well:
WebDriver driver = new FirefoxDriver();
String startURL = //a starting url;
String currentURL = null;
WebDriverWait wait = new WebDriverWait(driver, 10);
foo(driver,startURL);
/* go to next page */
if(driver.findElement(By.xpath("//*[#id='someID']")).isDisplayed()){
String previousURL = driver.getCurrentUrl();
driver.findElement(By.xpath("//*[#id='someID']")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
ExpectedCondition e = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return (d.getCurrentUrl() != previousURL);
}
};
wait.until(e);
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
}
I hope your problem will be resolved using above code

Related

ElementNotInteractableException: element not interactable in Selenium Java

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class E2E {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.flygofirst.com/");
driver.findElement(By.xpath("//span[#id='onewaymodal-id']")).click();
Thread.sleep(25000);
driver.findElement(By.xpath("//div[#id = 'oneWaybd']//div[#class ='fromTo']/div[1]")).sendKeys("Ch");
}
}
I Tried to SendKeys in the From Text Box i was getting Element not Intractable.
And I provided Waiting time so that all the web elements will load. After that also i got the same exception Element not Intractable.
Can anyone help me with this
You have to modify the locator, try this one, its working:
// to handle the Accept Cookies button
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#cookie-btn"))).click();
driver.findElement(By.xpath("//span[#id='onewaymodal-id']")).click();
Thread.sleep(1000);
// modified the below locator
driver.findElement(By.xpath("(.//div[#class='fromTo']//input[#id='roundTripbdFromView'])[2]")).sendKeys("ch");
I answered your previous question also, check that, if it works, mark that as the answer.

Web page stuck with loading bar while executing the automation script using Selenium?

I'm executing the automation script, in which I am facing loading bar for infinite time on the specific web page.
I have confirmed that issue is not from script side because earlier same scripts are executing fine.
I have applied the solutions as below.
Executing the script in other browsers
Increase wait time
Updated Chrome browser/chromedriver.exe
Currently I'm using below tools/version
Chrome Version: 89.0.4389.82
Chrome driver[Version:ChromeDriver89.0.4389.23]
Java [version: 11]
Selenium WebDriver
Can anyone please provide me the solutions?
Thanks in advance
Try changing the page load strategy to "eager":
This will make Selenium WebDriver to wait until the initial HTML document has been completely loaded and parsed, and discards loading of stylesheets, images and subframes.
When set to eager, Selenium WebDriver waits until DOMContentLoaded event fire is returned.
Example usage:
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chrome.ChromeDriver;
public class pageLoadStrategy {
public static void main(String[] args) {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver = new ChromeDriver(chromeOptions);
try {
// Navigate to Url
driver.get("https://google.com");
} finally {
driver.quit();
}
}
}

MSEdge Driver not launching Brwoser using selenium

I am using selenium to automate MS Edge (chromium) browser. I have downloaded correct driver i.e v79.0.309.43 which is same for driver and browser.
but when I run code , it simply shows message that it is launching on multiple ports .See Screenshot below=
Can someone point out what is issue here?
Thanks,
Nilesh
I test with Microsoft Edge(Chromium) Beta version 79.0.309.43 and the same version of Microsoft Edge(Chromium) WebDriver and it works. You could refer to the code below and change the path to your owns:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeOptions;
public class Edgeauto {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "your\\path\\to\\edge\\webdriver\\msedgedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("C:\\Program Files (x86)\\Microsoft\\Edge Beta\\Application\\msedge.exe");
EdgeOptions edgeOptions = new EdgeOptions().merge(chromeOptions);
WebDriver driver = new ChromeDriver(edgeOptions);
driver.get("https://www.google.com/");
}
}
Also please remember to have the location of Edge Beta and msedgedriver.exe on your PATH.

Chrome browser version - 72.0.3626.121 not opening with selenium

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import init.Constants;
public class TestSelenium {
private static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+Constants.getChromeDriver());
driver = new ChromeDriver();
driver.get("https://www.google.com");
}
}
I am getting the error as below
Starting ChromeDriver 2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1) on port 45163
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
The chrome browser is opening but the url is not coming up.
I am using
Chrome driver - 72.0.3626.69
WebDriver - 3.0
You can use bonigarcia dependency for your automation. Then you don't need to keep chromedriver.exe or setting up system variables. It will do all the configurations automatically for all the platforms and all the browsers as well.
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.3.0</version>
</dependency>
Below is a sample class to get chrome browser instance. You can modify this class as per your requirement.
import io.github.bonigarcia.wdm.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DriverFactory {
public static WebDriver getDriver() {
WebDriverManager.chromedriver().setup();
return new ChromeDriver();
}
}
I have tested this with Selenium 3.14.0 and Chrome Version 73.0.3683.86 (Official Build) (64-bit)
You mentioned using Chrome driver - 72.0.3626.69 but error shows Starting ChromeDriver 2.46.628402. Check if you have correct chrome driver.
The possible reasons:
Old selenium (download 3.14.xx from https://www.seleniumhq.org/download/)
older chrome driver (consider update to the latest chromedriver https://chromedriver.storage.googleapis.com/index.html?path=73.0.3683.68/)
Chrome browser version mismatch (check the browser version and chromedriver compatibility at https://sites.google.com/a/chromium.org/chromedriver/downloads/version-selection)
Older Java version (latest Java version 11.0.2)
it does not open because you have not specify the path of the chromedriver.exe file
please find the below code snippet.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestChrome {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path of the exe file\\chromedriver.exe");
// Initialize browser
WebDriver driver = new ChromeDriver();
// Open facebook
driver.get("http://www.facebook.com");
// Maximize browser
driver.manage().window().maximize();
}
}
Try to first set the Chrome driver path before calling the Chrome driver.
System.setProperty("webdriver.chrome.driver", "path of the exe file\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.google.com");
Try to set chrome options:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--whitelist-ip *");
chromeOptions.addArguments("--proxy-server='direct://'");
chromeOptions.addArguments("--proxy-bypass-list=*");
WebDriver driver = new ChromeDriver(chromeOptions);
Step 1: Check your browser version with your webdriver version on this page: https://chromedriver.chromium.org/downloads
Step 2: If after followed above step till you have the same issue then follows the below method:
public class TestSelenium {
private static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","user.dir");
driver = new ChromeDriver();
driver.get("https://www.google.com");
}
}

WebDriver not opening URL

I'm very new to selenium so I'm having trouble spotting the problem with my code. I'm using a webDriver backed selenium object, it starts the driver but never opens the URL and the driver just closes after a few moments. The last time this happened to me it was just because I had left "http" out of the URL. So what's causing it this time?
public void testImages() throws Exception {
Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.testsite.com/login");
System.out.println(selenium.getXpathCount("//img"));
}
The setup looks like:
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
Thread.sleep(2000);
}
The teardown method just consists of driver.close().
I'm using selenium 2.14 and the testNG Eclipse plug-in.
You might need to do the following
selenium.open("www.testsite.com/login");
Check out this example from the selenium site:
// You may use any WebDriver implementation. Firefox is used here as an example
WebDriver driver = new FirefoxDriver();
// A "base url", used by selenium to resolve relative URLs
String baseUrl = "http://www.google.com";
// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
// Perform actions with selenium
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
// Get the underlying WebDriver implementation back. This will refer to the
// same WebDriver instance as the "driver" variable above.
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
//Finally, close the browser. Call stop on the WebDriverBackedSelenium instance
//instead of calling driver.quit(). Otherwise, the JVM will continue running after
//the browser has been closed.
selenium.stop();
link to selenium
You would need to add driver.get(url) like below.
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.testsite.com/login");
}