Java mute Selenium webdriver - java

I am working on making a tiny Java app of which needs to open a Firefox browser in hidden mode and also have it be muted.
As you can see by the code below, I have found a way to have the browser be hidden. However I am having some issues figuring out, how to mute the browser and whether it is actually even possible to do.
Considering the following answer I figured it may be possible, I know the answer is for Python but it's using the selenium webdriver, however for Chrome, where I need it for Firefox.
I tried to insert the following line firefoxBinary.addCommandLineOptions("--mute-audio"); however that didn't seem to work.
The following is what I currently have.
WebDriver driver;
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
System.setProperty("webdriver.gecko.driver", driverPath);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
driver = new FirefoxDriver(firefoxOptions);

After further investigation, the following code fixed the issue of muting Geckodriver in Java, using Selenium.
I added this code below the firefoxOptions.setBinary(firefoxBinary); line of my script above.
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("media.volume_scale", "0.0");
firefoxOptions.setProfile(firefoxProfile);

Related

How to launch Edge browser in IE mode using java selenium?

We have the options to open edge in ie mode using c#. Not it selenium java
Code -
WebDriver driver;
System.setProperty("webdriver.ie.driver", "D:\\SG\\Downloads\\IEDriverServer_Win32_3.150.2\\IEDriverServer.exe");
InternetExplorerDriverService ieService= InternetExplorerDriverService.createDefaultService();
InternetExplorerOptions ieOptions= new InternetExplorerOptions();
ieOptions.setCapability("ie.edgechromium", true);
ieOptions.setCapability("ie.edgepath", "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
driver= new InternetExplorerDriver(ieOptions);
Currently the option of launching Edge browser in IE Mode is only available via the C# bindings for Selenium. You will not be able to achieve this using Java bindings.
Reference - Please see the answers here and also comment from one of the committers here ( see the comments in the question)

Selenium WebDriver - Java - Changing the User Agent

i am currently going through selenium webdriver on the selenium wbesite.
There is a section of the website that talks about the "Change user agent" and explains how to do it: https://docs.seleniumhq.org/docs/03_webdriver.jsp#chapter03-reference However, no "real" or "mock" example is given showing this in "action".
So, I was wondering if anyone here, would perhaps have or be able to share a code snippet, with the project included, showing this "in action"? A mock project, you may have or designed yourself to practice this, should suffice.
I have already made a lot of research online, trying to find an example of this, but no success.
From the link you provided, you can change the User-Agent with below code:
Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override", "user-agent-string");
WebDriver driver = new FirefoxDriver(profile);
Chrome:
ChromeOptions chrome = new ChromeOptions();
chrome.addArguments("user-agent=YOUR_USER_AGENT");
It's Java but you can easily switch to any selenium-supported language because all of them implement FirefoxProfile and ChromeOptions

window.chrome.runtime is undefined for selenium webdriver

I am using Selenium for testing. I noticed a different behavior starting Chrome manually and starting it with selenium. After a lot of investigation I broke the problem down to JavaScript's window.chrome.runtime which is undefined if started with selenium.
After some research on Google I have found people facing similar issues, but none of their solutions worked for me.
I have tried so far to remove the test-type switch:
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Arrays.asList("test-type"));
Are there any other ways I can make it work?
Answer which did the job for me was at https://groups.google.com/forum/#!topic/chromedriver-users/7wF9EHF2jxQ
Code snippet:
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Arrays.asList("test-type"));
Selenium does not start browser with an existing browser profile, it create a temporary one, every single time. So it does not need to send js to check any installed plugins. To avoid the undefined runtime, use an existing browser profile.
ChromeOptions options = new ChromeOptions();
// edit this path
options.addArguments("user-data-dir=C:\\Users\\pburgr\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(options);

Disabling/Enabling Pop up in browsers using Selenium WebDriver with Java

In my application, I need to write code for enabling/disabling of browser pop up for some scenario. I searched on wed and tried many solutions but non of them seems to be working.
It will be very helpful for everyone if some one can provide solution of this problem in one discussion thread.
I am able to handle that is Chrome but I other not seems to be working. My code is:-
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
options.addArguments("chrome.switches","--disable-extensions");
driver = new ChromeDriver(options);

Loading Chrome Profile Not Working (ChromeDriver)

I'm trying to load my Chrome profile for use with Selenium WebDriver but it doesn't work and always defaults to a new one. I tried the codes provided from here.
Is there any workaround or fix? I'd like to run ChromeDriver with customized extensions, options and whatnot since command line flags and ChromeOptions have restricted functionality.
Also, I'm having another issue about loading preconfigured extensions.
I had the same problem.But I did something like this and it worked.
String userProfile= "C:/Users/"your user"/AppData/Local/Google/Chrome/User";
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=" + userProfile);
driver = new ChromeDriver(options);
Please let me know if it work also for you.

Categories