Selenium Chromedriver opens with data:, in navigation bar - selenium

if i open Chrome via chromedriver and navigate to a URL i only get a data:, in the navigation bar. All googled solutions (right chromedriverversion, protocoll in URL, etc.) didnt help me.
package de.vhv.selenium;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class OpenChromeAndNavigate {
#Test
public void test() {
System.setProperty("webdriver.chrome.driver", "C://vhventw//selenium//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.de");
}
}
In addition everything works if i add --headless and listen to the debugport. But i dont want to let it run headless.
package de.vhv.selenium;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class OpenChromeAndNavigate {
#Test
public void test() {
System.setProperty("webdriver.chrome.driver", "C://vhventw//selenium//chromedriver.exe");
WebDriver driver = new ChromeDriver(getDesiredCapabilities());
driver.get("https://www.google.de");
}
private ChromeOptions getDesiredCapabilities() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
// options.addArguments("--disable-extensions"); // disabling extensions
// options.addArguments("--disable-gpu"); // applicable to windows os only
// options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
// options.addArguments("--no-sandbox");
options.addArguments("--remote-debugging-port=9223");
return options;
}
}
Any ideas what i can try?
Setup:
Chrome Version = 71.0.3578.80
Chromedriver Version = 2.46.628402

I figured out, that the line
options.addArguments("--remote-debugging-port=9225");
fixed my problem. I already used it in headless runs to listen to the port and watch the headless run. But it fixed my problem with headfull runs.
return new ChromeDriver(getDesiredCapabilities());
private ChromeOptions getDesiredCapabilities() {
ChromeOptions options = new ChromeOptions();
//options.addArguments("--headless");
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox");
options.addArguments("--remote-debugging-port=9225");
return options;
}

You need to take care of a couple of things:
Not sure about your project structure but I will suggest to avoid the . character and the word selenium within the package name as in:
package de.vhv.selenium;
The Value part passed through System.setProperty() line containing the absolute path of chromedriver.exe should be expressed through escaped backslashes as:
System.setProperty("webdriver.chrome.driver", "C:\\vhventw\\selenium\\chromedriver.exe");
As per ChromeDriver - WebDriver for Chrome:
If you are using Chrome version 72, please download ChromeDriver 2.46 or ChromeDriver 72.0.3626.69
As per best practices:
Upgrade JDK to recent levels JDK 8u202.
Upgrade Selenium to current levels Version 3.141.59.
Upgrade ChromeDriver to current ChromeDriver v73.0.3683.68 level.
Keep Chrome version between Chrome v73 levels. (as per ChromeDriver v73.0.3683.68 release notes)
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your #Test.
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.

I was struggling with the same issue, then I realised that I forgot to start the test with :
$I->amOnPage('/');
definitely to try before digging deeper.

Related

Download file through Google Chrome RemoteWebDriver- headless mode in Linux using Selenium Java [duplicate]

I'm do me code in Cromedrive in 'normal' mode and works fine. When I change to headless mode it don't download the file. I already try the code I found alround internet, but didn't work.
chrome_options = Options()
chrome_options.add_argument("--headless")
self.driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'{}/chromedriver'.format(os.getcwd()))
self.driver.set_window_size(1024, 768)
self.driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': os.getcwd()}}
self.driver.execute("send_command", params)
Anyone have any idea about how solve this problem?
PS: I don't need to use Chomedrive necessarily. If it works in another drive it's fine for me.
First the solution
Minimum Prerequisites:
Selenium client version: Selenium v3.141.59
Chrome version: Chrome v77.0
ChromeDriver version: ChromeDriver v77.0
To download the file clicking on the element with text as Download Data within this website you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
options.add_argument("--window-size=1920,1080")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe', service_args=["--log-path=./Logs/DubiousDan.log"])
print ("Headless Chrome Initialized")
params = {'behavior': 'allow', 'downloadPath': r'C:\Users\Debanjan.B\Downloads'}
driver.execute_cdp_cmd('Page.setDownloadBehavior', params)
driver.get("https://www.mockaroo.com/")
driver.execute_script("scroll(0, 250)");
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#download"))).click()
print ("Download button clicked")
#driver.quit()
Console Output:
Headless Chrome Initialized
Download button clicked
File Downloading snapshot:
Details
Downloading files through Headless Chromium was one of the most sought functionality since Headless Chrome was introduced.
Since then there were different work-arounds published by different contributors and some of them are:
Downloading with chrome headless and selenium
Python equivalent of a given wget command
Now the, the good news is Chromium team have officially announced the arrival of the functionality Downloading file through Headless Chromium.
In the discussion Headless mode doesn't save file downloads #eseckler mentioned:
Downloads in headless work a little differently. There's the Page.setDownloadBehavior devtools command to set a download folder. We're working on a way to use DevTools network interception to stream the downloaded file via DevTools as well.
A detailed discussion can be found at Issue 696481: Headless mode doesn't save file downloads
Finally, #bugdroid revision seems to have nailed the issue for us.
[ChromeDriver] Added support for headless mode to download files
Previously, Chromedriver running in headless mode would not properly download files due to the fact it sparsely parses the preference file given to it. Engineers from the headless chrome team recommended using DevTools's "Page.setDownloadBehavior" to fix this. This changelist implements this fix. Downloaded files default to the current directory and can be set using download_dir when instantiating a chromedriver instance. Also added tests to ensure proper download functionality.
Here is the revision and commit
From ChromeDriver v77.0.3865.40 (2019-08-20) release notes:
Resolved issue 2454: Headless mode doesn't save file downloads [Pri-2]
Solution
Update ChromeDriver to latest ChromeDriver v77.0 level.
Update Chrome to Chrome Version 77.0 level. (as per ChromeDriver v76.0 release notes)
Note: Chrome v77.0 is yet to be GAed/pushed for release so till then you can download and install a development build and test either from:
Chrome Canary
Latest build from the Dev Channel
Outro
However Mac OSX users have a wait for their pie as On Chromedriver, headless chrome crashes after sending Page.setDownloadBehavior on MacOSX.
Chomedriver Version: 95.0.4638.54
Chrome Version 95.0.4638.69
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
options.add_argument("--start-maximized")
options.add_argument("--no-sandbox")
options.add_argument("--disable-extensions")
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--disable-gpu")
options.add_argument('--disable-software-rasterizer')
options.add_argument("user-agent=Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166")
options.add_argument("--disable-notifications")
options.add_experimental_option("prefs", {
"download.default_directory": "C:\\link\\to\\folder",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False
}
)
What seemed to work was that I used "\\" instead of "/" for the address. The latter approach didn't throw any error, but didn't download any documents either. But, using double back slashes did the job.
For javascript use below code:
const chrome = require('selenium-webdriver/chrome');
let options = new chrome.Options();
options.addArguments('--headless --window-size=1500,1200');
options.setUserPreferences({ 'plugins.always_open_pdf_externally': true,
"profile.default_content_settings.popups": 0,
"download.default_directory": Download_File_Path });
driver = await new webdriver.Builder().setChromeOptions(options).forBrowser('chrome').build();
Then switch tabs as soon as you click the download button:
await driver.sleep(1000);
var Handle = await driver.getAllWindowHandles();
await driver.switchTo().window(Handle[1]);
This C# works for me
Note the new headless option https://www.selenium.dev/blog/2023/headless-is-going-away/
private IWebDriver StartBrowserChromeHeadlessDriver()
{
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--headless=new");
chromeOptions.AddArgument("--window-size=1920,1080");
chromeOptions.AddUserProfilePreference("download.default_directory", downloadFolder);
var chromeDownload = new Dictionary<string, object>
{
{ "behavior", "allow" },
{ "downloadPath", downloadFolder }
};
var driver = new ChromeDriver(driverFolder, chromeOptions, TimeSpan.FromSeconds(timeoutSecs));
driver.ExecuteCdpCommand("Browser.setDownloadBehavior", chromeDownload);
return driver;
}
import pathlib
from selenium.webdriver import Chrome
driver = Chrome()
driver.execute_cdp_cmd("Page.setDownloadBehavior", {
"behavior": "allow",
"downloadPath": str(pathlib.Path.home().joinpath("Downloads"))
})
I don't think you should be using the browser for downloading content, leave it to Chrome developers/testers.
I believe you should rather get href attribute of the element you want to download and obtain it using requests library
If your site requires authentication you could fetch cookies from the browser instance and pass them to requests.Session.

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");
}
}

Selenium commands not working in Chrome web driver (working with firefox)

I'm writing integration/e2e tests and for some reason any selenium driver commands don't see to be working with chromedriver, but they are working flawlessly with firefox driver and the firefox headless driver.
Commands tried: moveByOffset, and doubleClick
Tried both Geb's Interact method
interact {
doubleClick(centerClickable)
}
and accessing the webdriver directly:
def driver = browser.getDriver()
Actions action = new Actions(driver)
WebElement element= driver.findElement(By.className("vis-drag-center"))
def doubleclick = action.doubleClick(element).build()
doubleclick.perform()
Both methods work with the firefox driver. Neither work with chrome driver.
GebConfig.groovy file is set up as thus:
import io.github.bonigarcia.wdm.WebDriverManager
import org.openqa.selenium.Dimension
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxOptions
def chromeWebDriverVersion = '70.0.3538.67'
def driverFirefox = {
WebDriverManager.firefoxdriver().setup()
def driver = new FirefoxDriver()
driver.manage().window().setSize(new Dimension(width, height))
return driver
}
// ChromeDriver reference: https://sites.google.com/a/chromium.org/chromedriver/
// Download and configure ChromeDriver using https://github.com/bonigarcia/webdrivermanager
def driverChrome = {
WebDriverManager.chromedriver().version(chromeWebDriverVersion).setup()
def driver = new ChromeDriver()
driver.manage().window().setSize(new Dimension(width, height))
return driver
}
environments {
firefox {
driver = driverFirefox
}
chrome {
driver = driverChrome
}
//driver = driverFirefox
driver = driverChrome
I also tried version 2.43 of chrome.
Additional information:
Mac Mojave
Selenium v 3.7.0
geb v 2.2
spockcore v 1.1-groovy-2.4
groovy v 2.4.5
webdrivermanager v 3.0.0
If anyone is interested, what the test is doing: Selecting a vis.js element by clicking on it. Sleeping for a second (code not included here), then opening/activating it by double clicking it. Or dragging it.
Apart from the selenium actions everything works fine with chromedriver and geb. It's only now that I need the doubleClick and moveByOffset (not move to an element!) that I'm getting issues getting things to work properly
I found a similar question on here, might be the same issue. Maybe not. But there's no solution provided: Selenium Web Driver DragAndDropToOffset in Chrome not working?
Any help is hugely appreciated.
Thank you for your response kriegaex.
Your tests work for me as well. This leads me to think there's just some lower-level interaction between differences in how selenium's chromedriver and firefox driver have implemented the doubleclick and dragAndDropBy actions + the way our application responds to commands.
For any other people observing similar behaviour, I use a work-around where I add an additional action for chromedriver. Perhaps it's nicer to actually find out which KEYDOWN events etc. you should be using and fire those, or perhaps find out why application isn't responding to these events. But I feel like enough time is spent on this already :)
if (browser.getDriver().toString().contains("chrome")) {
// Work-around for chromedriver's double-click implementation
content.click()
}
interact {
doubleClick(content)
}
And for the dragAndDropBy:
def drag(Navigator content, int xOff, int yOff) {
//Work-around: move additional time for when chrome driver is used.
int timesToMove = browser.getDriver().toString().contains("chrome") ? 2 : 1
interact {
clickAndHold(content)
timesToMove.times {
moveByOffset(xOff, yOff)
}
release()
}
}
I just had a little bit of time and was curious because I never tried to perform a double-click in any of my tests before. So I used this page as a test case and ran the following test with both Firefox and Chrome drivers:
package de.scrum_master.stackoverflow
import geb.spock.GebReportingSpec
import org.openqa.selenium.By
import org.openqa.selenium.interactions.Actions
class DoubleClickTest extends GebReportingSpec {
def "double-click via Geb interaction"() {
given:
go "https://demo.guru99.com/test/simple_context_menu.html"
def doubleClickButton = $("button", text: "Double-Click Me To See Alert")
expect:
withAlert {
interact {
doubleClick(doubleClickButton)
}
} == "You double clicked me.. Thank You.."
}
def "double-click via Selenium action"() {
given:
go "https://demo.guru99.com/test/simple_context_menu.html"
def doubleClickButton = driver.findElement(By.tagName("button"))
def doubleClick = new Actions(driver).doubleClick(doubleClickButton).build()
expect:
withAlert {
doubleClick.perform()
} == "You double clicked me.. Thank You.."
}
}
It works flawlessly, both ways of double-clicking trigger the expected Javascript alert.
I am not even using the latest driver version 2.45 but 2.41 against Chrome 71 64-bit on Windows 10. Besides, I also use bonigarcia's Webdriver Manager. I have no idea what is wrong with your setup. My Selenium version is 3.14.0, a bit newer than yours, Geb 2.2, Spock 1.1-groovy-2.4, Groovy 2.4.7.
Maybe it is a MacOS thing? I cannot verify that. Maybe you just run my test first, then maybe upgrade your Selenium and if that also does not help try to downgrade the Chrome driver in order to find out if the problem could be driver version related.
Update: I upgraded to Chrome driver 2.45, the test still works.
Update 2022-02-16: Updated the test to work with another example page, because the old URL still exists, but the Javascript there no longer works.

Selenium Web Driver firefox not responding

I am using simple selenium example using Web driver classes, but the IE web driver class working fine, but the Firefox is not responding not opening browser and not throwing any error in console.
code is here
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GoogleSearchFF {
public static void main(String args[]){
WebDriver driver=new FirefoxDriver();
System.out.println("Loading Google search page");
driver.get("http://www.google.com");
System.out.println("Google search page loaded fine");
}
}
selenium jar files added to classpath..
\selenium-java-2.13.0\selenium-2.13.0\selenium-java-2.13.0.jar
\selenium-java-client-driver-1.0.1\selenium-java-client-driver.jar
\Selenium Latest\selenium-server-standalone-2.13.0.jar
any jar is missing?
The code works for IE by setting proeprty INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS = true
Downgrade the Firefox version to 8 as Selenium 2.13.0 supports Firefox versions upto 8 only.
For reference check this log.
Instead of downgrading Firefox to 8,
You need to download the geckodriver.exe and set the System.property() by
System.setProperty("webdriver.gecko.driver", "pathTogeckodriver");
before calling WebDriver driver = new FirefoxDriver();