We are facing issue in switching to the new window on safari browser. Below is our code used for switching the window.
public void switchToWindow() {
Set<String> availableWindows = driver.getWindowHandles();
for (String strWinHandle : availableWindows) {
driver.switchTo().window(strWinHandle);
}
}
In availableWindows, it returns all window handles but instead of switching to new window, it is switching to parent window.
Above code works fine on all other browsers.
Selenium version - 3.11.0
Safari version - 11.1.1
You can try the following code.
public void switchToWindow() {
String curWinHandle = driver.getWindowHandle();
Set<String> availableWindows = driver.getWindowHandles();
for (String strWinHandle : availableWindows) {
if(!curWinHandle.equals(strWinHandle))
driver.switchTo().window(strWinHandle);
}
}
Am running the Selenium webdriver(Maven Project) Scripts through Jenkins by calling pom.xml file. I have set below mentioned browser resolution
driver.manage().window().maximize();
Dimension defaultSize = new Dimension(2560,1440);
driver.manage().window().setSize(defaultSize);
However, while running the scripts through Jenkins, Chrome browser window is not set to the new dimension.
Note : In Local machine it's working fine..
Copied from my question - GOCD pipeline, Selenium ChromeDriver window size is not set
his issue looks to have been caused by two issues,
1- When mvn clean test is run from the IDE this process runs under your current user. However, when run by the Continuous Integration environment, the process is owned by the System process. so does not have the same access to resources.
2 When run from the IDE, chrome will pop up. When run from the CI environment I assumed that it defaulted chrome to run in headless mode. It does not, you have to set the --headless argument so my configuration that now works is as follows
public class GoogleChrome extends Base {
private static final Logger logger = LogManager.getLogger(GoogleChrome.class);
private String rootPath = System.getProperty("user.dir").replace("\\","/");
#Autowired
protected WebDriver driver;
public WebDriver startChromeDriver() {
logger.info("Chrome driver path : " + rootPath + "/../Tools/Drivers/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", rootPath + "/../Tools/Drivers/chromedriver.exe");
Map<String, Object> prefs = new HashMap<String, Object>();
logger.info("Disabling Chrome's credentials service");
prefs.put("credentials_enable_service", false);
logger.info("Disabling Chrome's password manager");
prefs.put("password_manager_enabled", false);
final String regex = "^\\D*$";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(System.getProperty("user.name"));
boolean isHuman = matcher.matches();
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
logger.info("Disabling Chrome's info bars");
options.addArguments("disable-infobars");
options.addArguments("--incognito");
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
options.addArguments("--allow-insecure-localhost");
if (isHuman){
logger.info("Chrome starting maximized - isHuman: " +isHuman + " process run by " +System.getProperty("user.name"));
options.addArguments("--start-maximized");
} else {
logger.info("Chrome starting headless - isHuman: " +isHuman + " process run by " +System.getProperty("user.name")) ;
options.addArguments("--headless");
options.addArguments("--window-size=1980,1080");
}
options.setAcceptInsecureCerts(true);
try {
logger.info("Killing Chrome browser");
Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
} catch (IOException e) {
logger.error("Task Kill IOException : " + e.getMessage());
}
logger.info("Starting Chrome browser...");
sleep(2);
driver = new ChromeDriver(options);
logger.info("Window size: "+ driver.manage().window().getSize());
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
return driver;
}
}
Chrome disallows setting the size larger than the system's screen resolution width, the problem may be this in your case. Some workaround could be to maximize the window to at least have the largest window available on that system.
While sometimes old chromedriver also cause the issue
If still problem exist then you can change the resolution by chrome option as :
chromeOptions.addArguments("window-size=1936,1056");
Full code will be like :-
System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("window-size=1936,1056");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.co.in/");
Hope it will help you :)
Want to disable automatic geoloaction in Chrome using Chromedriver when I visit a https website.
Tryed:
from selenium.webdriver.chrome.options import Options
chromeOptions = webdriver.ChromeOptions()
prefs = {"profile.default_content_settings.geolocation" : "2"}
chromeOptions.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
And:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=chrome_options)
Both doesn't work because on every new chrome window created with chromedriver the geolocation is enabled.
your pref key is incorrect, below code worked for me
options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.geolocation" :2}
options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=options)
Below code is working for me on July 2018
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.managed_default_content_settings.geolocation", 2);
options.setExperimentalOption("prefs", prefs);
ChromeDriver = new ChromeDriver(options);
Saludos.
Below is my non-working code for the Firefox profile.
#Before
public void setUp() throws Exception {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.dir", "/Location");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/docx");
driver=new FirefoxDriver(profile);
baseUrl = "<URL>";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void test1() throws Exception {
driver.get("URL");
driver.findElement(By.id("Username")).sendKeys("username");
driver.findElement(By.id("Password")).sendKeys("Password");
driver.findElement(By.xpath(".//*[#id='rowCell0']/td[4]/a[4]")).click();// This line of code is for Download Link on the UI
Now once selenium clicks on it, Firefox will again open the confirmation box asking for "Open with" and "Save File".
Please remove
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting",false);
and try it should work...let me know
I met this issue too and it blocked me several days!
Please make sure the file type you are trying to download, and then configure with the correct value in below line:
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/docx");
Your site source code is sending back a content header saying the file is xxx to match the preferences you have set.
To reference, the types may be: application/octet-stream, application/vnd.ms-excel, text/csv, text/plain, application/zip, application/exe, application/x-zip, application/x-zip-compressed, application/download
Trying to find a way to disable Firefox from raising a warning every time a connection uses an "untrusted" certificate, with Selenium. I believe that the kind of solution that would work the best would be to set one of the browser preferences.
Just found this from the Mozilla Foundation bug link and it worked for me.
caps.setCapability("acceptInsecureCerts",true)
I found this comment on enabling this functionality in Selenium for Java. There is also this StackOverflow question about the same issue, also for Java For Python, which was my desired target language, I came up with this, through browsing the FirefoxProfile code:
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
Which, as far as I have tested, has produced the expected behavior.
Hope this helps somebody!
No need of custom profiles to deal with "Untrusted connection" on WebDriver
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new FirefoxDriver(capabilities);
None of the above answers worked for me. I'm using:
https://github.com/mozilla/geckodriver/releases/download/v0.12.0/geckodriver-v0.12.0-win64.zip
Firefox 50.1.0
Python 3.5.2
Selenium 3.0.2
Windows 10
I resolved it just by using a custom FF profile which was easier to do than I expected. Using this info https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager on how to make a custom profile, I did the following:
1) Made a new profile
2) Manually went to the site in FF to raise the untrusted certificate error
3) Add a site exception (when the error is raised click advanced and then add exception)
4) confirm the exception works by reloading the site (you should no longer get the error
5) Copy the newly create profile into your project (for me it's a selenium testing project)
6) Reference the new profile path in your code
I didn't find any of the following lines resolved the issue for me:
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['handleAlerts'] = True
firefox_capabilities['acceptSslCerts'] = True
firefox_capabilities['acceptInsecureCerts'] = True
profile = webdriver.FirefoxProfile()
profile.set_preference('network.http.use-cache', False)
profile.accept_untrusted_certs = True
But using a custom profile as mentioned above did.
Here is my code:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
#In the next line I'm using a specific FireFox profile because
# I wanted to get around the sec_error_unknown_issuer problems with the new Firefox and Marionette driver
# I create a FireFox profile where I had already made an exception for the site I'm testing
# see https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager
ffProfilePath = 'D:\Work\PyTestFramework\FirefoxSeleniumProfile'
profile = webdriver.FirefoxProfile(profile_directory=ffProfilePath)
geckoPath = 'D:\Work\PyTestFramework\geckodriver.exe'
browser = webdriver.Firefox(firefox_profile=profile, capabilities=firefox_capabilities, executable_path=geckoPath)
browser.get('http://stackoverflow.com')
From start to finish with all the trimmings, in C#. Note that I had installed FFv48 to a custom directory because GeckoDriver requires that specific version.
var ffOptions = new FirefoxOptions();
ffOptions.BrowserExecutableLocation = #"C:\Program Files (x86)\Mozilla Firefox48\firefox.exe";
ffOptions.LogLevel = FirefoxDriverLogLevel.Default;
ffOptions.Profile = new FirefoxProfile { AcceptUntrustedCertificates = true };
var service = FirefoxDriverService.CreateDefaultService(ffPath, "geckodriver.exe");
var Browser = new FirefoxDriver(service, ffOptions, TimeSpan.FromSeconds(120));
In my case I was using Marionette driver instead of Firefox driver. There is an acknowledged bug (https://bugzilla.mozilla.org/show_bug.cgi?id=1103196) for it. In the meantime I'm using Firefox driver instead:
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
dc.setCapability(FirefoxDriver.PROFILE, profile);
// this is the important line - i.e. don't use Marionette
dc.setCapability(FirefoxDriver.MARIONETTE, false);
Webdriver driver = new FirefoxDriver(dc);
C#: Something have changed as options now has own attribute for this.
var ffOptions = new FirefoxOptions();
ffOptions.AcceptInsecureCertificates = true;
Driver = new FirefoxDriver(ffOptions);
Hope this helps.
I added the below and then it worked for me
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setAcceptInsecureCerts(true);
WebDriver driver = new FirefoxDriver(desiredCapabilities);
For me, using PHP facebook/webdriver I set create a profile and authorised the certified. The name of profile was selenium.
Next I initialise my selenium 3:
java -jar -Dwebdriver.firefox.profile=selenium selenium-server-standalone-3.0.1.jar
Then in FirefoxDriver.php
I set const PROFILE = 'selenium';
This worked for me.
For Firefox driver and Java add these lines:
WebDriver driver;
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("default");
testprofile.setAcceptUntrustedCertificates(true);
testprofile.setAssumeUntrustedCertificateIssuer(true);
driver = new FirefoxDriver(testprofile);
If you use geckodriver don't forget to add this before profile initialization:
System.setProperty("webdriver.gecko.driver","<PATH_TO_GECKODRIVER>\\geckodriver.exe");
In Java you have to use DesiredCapabilities.setAcceptInsecureCerts(). To get a FirefoxDriver with custom capability and profile do the following:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setAcceptInsecureCerts(true);
FirefoxProfile profile = new FirefoxProfile();
profile.set*...
FirefoxOptions options = new FirefoxOptions();
options.addCapabilities(capabilities);
options.setProfile(profile);
new FirefoxDriver(options);
In my case this did the trick
FirefoxOptions options = new FirefoxOptions();
options.addCapabilities(new ImmutableCapabilities(ImmutableMap.of(
CapabilityType.ACCEPT_SSL_CERTS, true,
CapabilityType.ACCEPT_INSECURE_CERTS, true)));
WebDriver driver = new FirefoxDriver(options);
Above solution worked for me on Firefox 54.0b9 (64-bit). This is my code.
Create your capabilities
Create FF Profile with your requirements
Add 1. & 2. to Firefox Options and pass it to FirefoxDriver
Like below
capabilities = new DesiredCapabilities().firefox();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
//Accept Untrusted connection and to download files
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
profile.setPreference("dom.file.createInChild", true);
profile.setPreference("browser.download.folderList", 1);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting"
,false);
profile.setPreference("pdfjs.disabled", true );
profile.setPreference("browser.helperApps.neverAsk.saveToDisk"
,"application/pdf;image/jpg;image/jpeg;text/html;text/plain;application/zip;application/download");
System.setProperty("webdriver.gecko.driver", config.getGeckoDriver());
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
FirefoxOptions options = new FirefoxOptions();
options.addCapabilities(capabilities);
options.setProfile(profile);
driver=new FirefoxDriver(options);
This configuration works for me in PHP
public function setUp()
{
$this->setHost('localhost');
$this->setPort(4444);
$this->setBrowserUrl('https://example.loc');
$this->setBrowser('firefox');
$this->setDesiredCapabilities(["acceptInsecureCerts" => true]);
}
For Firefox I run
java -jar selenium-server-standalone-3.8.1.jar -enablePassThrough false
I was having this issue useing Node JS and Selenium. Searched everywhere but could not find anything.
Finally got it. Maybe this will help someone.
var webdriver = require('selenium-webdriver');
driver = new webdriver.Builder()
.withCapabilities({'browserName': 'firefox', acceptSslCerts: true, acceptInsecureCerts: true})
.build()
for PHP please use below code
$serverUrl = 'http://localhost:4444/wd/hub';
$capabilities = new DesiredCapabilities(
[
'browserName' => 'firefox',
'proxy' => [
'proxyType' => 'manual',
'httpProxy' => 'localhost:0000',
'sslProxy' => 'localhost:XXXX',
],
]
);
$capabilities->setCapability('acceptInsecureCerts',True);
$driver = RemoteWebDriver::create($serverUrl, $capabilities,60*1000,60*1000);