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

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)

Related

How to fix the issue with very small screen size with Selenium webdriver

We just ran into a problem in our test automation infrastructure. The problem is that Windows defaults to a very small screen size (I assume 800x600) while running from Jenkins. Then if the website under test doesn't support such a small resolution without overlapping UI elements, the browser will fail some tests for items that are no longer visible. How can we solve this?
It would be best maximize your window as soon as you instantiate your web driver instance.
I don't know the setup of your code but please try adding the following line after you've created an instance of the web driver you wish to use:
Java
driver.manage().window().maximize();
C#
_driver.Manage().Window.Maximize();
Python
driver.maximize_window()
Ruby
#driver.manage.window.maximize
The above may not work in Chrome - if this is the case, please use:
ChromeOptions options = new ChromeOptions();
options.addArgument("--start-maximized");
driver = new ChromeDriver(options);
As per #Ardesco & #Fenio comments - you can also set a specific dimension using:
driver.manage().window().setSize(new Dimension(1920,1080));
driver.manage().window().fullscreen()
Within your driver setup you can change the browser window size using the following in C#
driver.Manage().Window.Size = new System.Drawing.Size(width: 1600, height: 1200);

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

Java mute Selenium webdriver

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

Selenium: Open Link in Same Tab

I am clicking a link via Selenium webdriver and the link opens up a new windows - I want to force the link to open in the same window (and the same tab) is this possible?
Most of the time this does not happen only with a specific link..
Thanks
Before clicking the link update the link's target property to self and then click it.
For updating the property please refer to this link.
See if the HTML code is like below, link will open in a different tab/windows depending upon the browser settings.
<a href = "#" target = "_blank">
When a Firefox browser is launched via Selenium Webdriver, the default profile it launches by default has this option enabled. You can create a new firefox profile by disabling this option. In this case the link will open in the same firefox window.
In Chrome driver, new links open in the same window itself.
You can force selenium webdriver to open a link in the same window but to open the link in same tab I don't think you can force it directly without injecting some Javascript. Using Javascript you can update the attribute target to complete your requirement.
If you want to inject Javascript you can use JavaScriptExecutor from Selenium Webdriver API.
((JavaScriptExecutor)driver).executeScript("document.getElementById('ID').setAttribute('target', 'self');")
For single-dom element
#driver.execute_script("document.querySelector('your_css_element')[#{index}].setAttribute('target','_blank')")
For multi-dom elements
your_list_of_elements.each_with_index do |elem, index|
#driver.execute_script("document.querySelectorAll('your_css_element')[#{index}].setAttribute('target','_blank')")
end
(note: the above is a ruby snippet, apply the same in your preferred lang.)
WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.link.open_newwindow", 1);
driver = new FirefoxDriver(options);

Open webdriver on a firefox already opened

I am looking for how to open selenium webdriver in a new tab of a firefox already opened. I aim to do that beacause when my java program (webdriver) open firefox, i have to set profile params in my program. To do this, i must every time log on in a dialog frame, because i have installed cntlm.
In short, I want to open firefox in a new tab.
Thaks a lot!
Please watch this Selenium feature ticket for progress.
Issue 18: Allow webdriver to attach to a running browser
Your summary "In short, I want to open firefox in a new tab." is ambiguous and confusing, if you are looking for how to open new tab, see this question.

Categories