setExperimentalOptions for Edge browser - java

I was able to change the download location in Chrome but when I do it for Edge browser the method setExperimentalOptions is not present for EdgeOptions. I am using Selenium 3.141.59 Java.
String location = System.getProperty("user.dir") + "\\Downloads";
HashMap preferences = new HashMap();
preferences.put("download.default_directory", location);
EdgeOptions options = new EdgeOptions();
options.setExperimentalOption("prefs", preferences); //setExperimentalOption is not existed for Edge
System.setProperty("webdriver.edge.driver","C:\\Users\\User\\Desktop\\Selenium\\Browsers\\Edge\\msedgedriver.exe");
WebDriver driver = new EdgeDriver(options);
driver.manage().window().maximize();
driver.get("https://file-examples.com/index.php/sample-documents-download/sample-doc-download/");
driver.findElement(By.xpath("//tbody/tr[1]/td[5]/a[1]")).click();

Selenium 3.141.59 the method setExperimentalOptions is undefined, after checking the Selenium EdgeDriver library I'm not finding any specific way to change the download location.
In this case, I can suggest you use the Selenium 4.0.0-beta-4 where the the .setExperimentalOption() is been defined.

Related

IE Mode in Edge supported by IEDriver not able to automate

since internet Explore is depreciated nowadays we have switched to edge browser but like you some sites support ie browser instead of edge so I need to run ie mode in edge browser here is an example of my code:
IE Mode in Edge supported by IEDriver
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//System.setProperty("webdriver.edge.driver", "D:\\Automation\\Jar Details\\Drivers\\edgedriver.exe");
System.setProperty("webdriver.ie.driver", "D:\\Automation\\Jar Details\\Drivers\\IEDriverServer.exe");
// System.setProperty("webdriver.ie.driver", "C:\\Users\\ofoxborn\\Desktop\\IE_Edge\\IEDriverServer.exe");
InternetExplorerDriverService ieService = InternetExplorerDriverService.createDefaultService();
DesiredCapabilities caps = new DesiredCapabilities();
caps.setAcceptInsecureCerts(false);
caps.setBrowserName("internet explorer");
caps.setCapability("ie.edgechromium", true);
System.out.println("caps.getBrowserName() = " + caps.getBrowserName());
System.out.println("caps browserName = " + caps.getCapability("browserName"));
caps.setCapability("ie.edgepath", "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
InternetExplorerOptions options = new InternetExplorerOptions();
options.enablePersistentHovering();
options.ignoreZoomSettings();
options.requireWindowFocus();
options.setCapability("ignoreProtectedModeSettings", true);
WebDriver driver = new InternetExplorerDriver(ieService, options.merge(caps));
driver.manage().window().maximize();
driver.get("https://github.com");
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Thread.sleep(5000);
System.out.println("caps.getBrowserName() = " + caps.getBrowserName());
System.out.println("caps browserName = " + caps.getCapability("browserName"));
driver.quit();
}
I don't know about it but when I run this the ie got invoked and run successfully I want it to start an edge browser I am using selenium 4.0.0 jar file and my ie version 10 is there something.
i am using ie driver 4.0.0
And so i done with setting in edge brwoserThis is edge browser seeting is there any window realted seeting i am misssing please help me and Thanks in advance
Well, I tested the problem and I think it is reasonable. In the official documentation it is stated that if you need to use Edge IE mode, one of the prerequisites is to enable IE11. Just refer to this doc: Edge IE mode-Prerequisites.
And as far as I know, IEdriver will start any version of IE installed in the machine, when Edge IE mode conditions are not met, it will not lanuch Edge, but default Internet Explorer.
So you could upgrade the version of IE to 11. I think this should make it work.

Is it possible to update Chrome WebDriver options?

Is it possible to change Chrome WebDriver ChromeOptions/Capabilities after creation of the WebDriver object?
Map<String, Object> preferences = new HashMap<String, Object>();
preferences.put("download.default_directory", "C:/downloads/");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", preferences);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
// do some stuff with the driver
// is it possible to change the default download directory here?
EDIT 1:
I have few hundred subpages on my website, each of them has few attachments. I would like to create the tree of the directories (each directory contains files connected only to this specific subpage).
Once you set the chromedriver options, its fixed to the driver instance, so it cannot be changed. An approach to going about this, will be to tear down the instance and create a new one with the updated path.

How to download a pdf file in chrome using selenium webdriver

I want to download pdf in chrome using selenium.
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")
+ System.getProperty("file.separator")
+ "BrowserDrivers"
+ System.getProperty("file.separator")
+ "chromedriver.exe");
String downloadFilepath = "C:\\Users\\Vinod\\Downloads";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
//Save Chrome Opions
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(cap);
driver.get(url);
I tried above code but its not working
Since chrome 57 the automatic pdf preview no longer works as a plugin, there's now a setting you can change to make this work. You can actually inspect the name of the pref by inspecting the chrome's own preference dialog, under "Content Settings" the flag that says "Open PDF files in the default PDF viewer application."
You can set that to false to avoid automatic pdf preview, like this (ruby example):
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {
'args' => ['disable-gpu', "--window-size=1920,1080"],
prefs: {
"download.prompt_for_download": false,
"download.directory_upgrade": true,
"plugins.always_open_pdf_externally": true,
"download.default_directory": DownloadHelpers::PATH.to_s
}
}
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: caps
)
Here are the C# options for anyone working with .NET
var tsTimeout = new TimeSpan(0, 5, 0);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", _downloadFolder);
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);
chromeOptions.AddUserProfilePreference("download.directory_upgrade", true);
chromeOptions.AddUserProfilePreference("plugins.plugins_disabled", "Chrome PDF Viewer");
chromeOptions.AddUserProfilePreference("plugins.always_open_pdf_externally", true);
_driver = new ChromeDriver(CWebCrawler.WebCrawlerRootFolder, chromeOptions, tsTimeout);
You might load this page and change the setting using selenium navigations:
chrome://settings/content/pdfDocuments
You would need to add below statement to your chrome profile:
chromePrefs.put("pdfjs.disabled", true);
It seems that newer versions of browsers are coming with built-in ability of displaying PDF files inside browser. Refer this for more information, though it is for firefox profile but still a good read.
Hope that solves your issue, else you may need to downgrade your chrome to make it work. Let me know if you have any queries.
Are you using Adobe Acrobat/Adobe Reader to display the PDFs? If so, it is probably Abode's behavior you need to modify.
Here are the steps I had to take:
Open Adobe Reader
Edit menu, Preferences
Selcet Internet from the Categories list
Uncheck Display PDF in browser
Press OK
you can also disable the Adobe plugin in Chrome, which will force Chrome to download the PDF.
It's under chrome://plugins.
but as you said its latest chrome browser then check is chrome PDF view visible in plugin if Yes disable it too.
Disable "Chrome PDF Viewer" or unmark always allowed to run check box try both
The following python code worked for me in Chrome to download pdf by disabling the pdf viewer.
options = webdriver.ChromeOptions()
prefs = {"download.default_directory": chromeDownloadPath,
"download.prompt_for_download": False,
"download.extensions_to_open": "applications/pdf",
"plugins.plugins_disabled": "Chrome PDF Viewer",
"plugins.always_open_pdf_externally": True}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome('chromedriver', options=options)

How to open current session Java Selenium?

I have seen many question, but could not implement any answer.
System.setProperty "webdriver.chrome.driver","chromedriverchromedriver.exe");
driver = new ChromeDriver();
// Then what exactly do I do?
//I want to open my regular Chrome window with all plugins and cookies
From http://code.google.com/p/selenium/wiki/ChromeDriver#Starting_Chromium_with_Specific_Flags:
Or to load with a specific profile (note that the default profile
directories can be found here):
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--user-data-dir=/path/to/profile/directory"));
WebDriver driver = new ChromeDriver(capabilities);

Load Chrome Profile using Selenium WebDriver using java

I'm having some troubles getting Selenium loading a chrome profile.
String pathToChrome = "driver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", pathToChrome);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
String chromeProfile = "C:\\Users\\Tiuz\\AppData\\Local\\Google\\Chrome\\User Data\\Default";
ArrayList<String> switches = new ArrayList<String>();
switches.add("--user-data-dir=" + chromeProfile);
capabilities.setCapability("chrome.switches", switches);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://www.google.com");
It starts great and works perfect, but I want to get the default profile loaded because I want to test it with some Extensions enabled and some preferences tested.
Does anybody has an idea why this code isn't working?
I've tested it with Selenium 2.29.1 and 2.28.0 with chromedriver 26.0.1383.0 on windows 7 x64.
This is an old question, but I was still having a problem getting it to work so I did some more research to understand what was happening. The answer from #PrashanthSams is correct, but I was incorrectly adding \Default to the end of the profile path
I found that Chrome appends \Default to the profile path specified in the user-data-dir. So if your profile path is specified as:
user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default\
it will append \Default and you will end up at:
C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default\Default
which is not the same as the profile that you would get if you opened chrome under that user profile normally.
You can also verify your settings if you open a command prompt, navigate to the chrome executable directory, and run chrome with the options specified similar to this:
chrome.exe --user-data-dir="C:\Users\user_name\AppData\Local\Google\Chrome\User Data"
Finally, you can go to a new tab in Chrome and browse to chrome://version/ you will see the actual profile that is being used. It will be listed like:
Profile Path C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default
These combinations did trick for me :)
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
You should try this
op.addArgument("--user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data/");
op.addArgument("--profile-directory=Profile 2");
Path where Chrome stores the profiles on Linux.
String chromeProfilePath = "/home/(user)/.config/google-chrome/Profile3/";
Creating ChromeOptions object, desabling the extensions and adding the profile I want to use by ".addArguments".
ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("chrome.switches", "--disable-extensions");
chromeProfile.addArguments("user-data-dir=" + chromeProfilePath);
As said above by JasonG, after this point Google-Chrome will append \Default to the String you've provided.
There is a "/Default" folder inside "/Profile3" directory, so what I did was...
I copied the content of "/Profile3" to the "/Default" folder.
Set the Browser System Properties and Path as you usually do, call the constructor that receives a ChromeOption and it will work fine.
WebDriver driver = new ChromeDriver(chromeProfile);
I copied default profile to any other folder and then I do connect to this copy
ChromeOptions options = new ChromeOptions();
options.AddArgument("--user-data-dir=C:\\AnyFolder");
driver = new ChromeDriver(options);
So it uses default profile
I tried in windows and following code works for me:
String userProfile= "C:\\Users\\user_name\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\";
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir="+userProfile);
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
How to know whether it is working ?
One way to know is to run the program twice without killing previous instance of the chrome. If the profile is valid, you'll see the second instance "as a new tab" in the first browser window. If it is not working, you get the second instance "as a new browser window".
According to the ChromeDriver wiki, this is a known issue and is currently not possible.
https://code.google.com/p/selenium/wiki/ChromeDriver
None of these above solutions works for me. And after several hours tirelessly research and trying, I already found out this solution
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
//chrome://version/
chromeOptions.addArguments("--user-data-dir=/home/{username}/.config/google-chrome");
//load default profile
chromeOptions.addArguments("--profile-directory=Default");

Categories

Resources