Unable to launch the Chrome headless browser in Windows 7 using Selenium - selenium-chromedriver

**Sinppet**
if (os.contains("win")) {
System.out.println("OS: WINDOWS");
System.setProperty("webdriver.chrome.driver", localPath+"chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("headless");
return new ChromeDriver(chromeOptions);
} else if (os.contains("mac")) {
System.out.println("OS: MAC");
System.setProperty("webdriver.chrome.driver", localPath+"chromedriver_mac64");
return new ChromeDriver();
}
Error I am facing
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //input[#id='user_id'] (tried for 180 second(s) with 500 MILLISECONDS interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:232)
at com.adp.marketplace.frwk.selenium.SeleniumFunctions.waitForVisibilityByXpath(SeleniumFunctions.java:510)
at com.adp.marketplace.prm.SSOTestInPRMSteps.The_user_logged_into_PRM_with_valid_cred(SSOTestInPRMSteps.java:28)
at ?.Given the user logged into PRM with valid creds(com/adp/marketplace/prm/SSOTestsInPRM.feature:7)

The argument for headless is wrong, should be :
chromeOptions.addArguments("--headless");
With -- before headless , just like you are adding arguments from shortcut :

Related

How to enable verbose logging in ChromeDriver? (GebConfig)

I have to enable verbose logging in Chrome / ChromeDriver to see why my geb tests are failing. Does anybody know how I can do that. Heres my GebConfig:
String chromeDriverDownloadFullPath = "https://chromedriver.storage.googleapis.com/${chromeDriverVersion}/${chromeDriverZipFileName}"
File chromeDriverLocalFile = downloadDriver(
currentPlatformName,
chromeDriverDownloadFullPath,
chromeDriverExecFileName,
'zip',
"chrome",
chromeDriverVersion)
System.setProperty('webdriver.chrome.driver', chromeDriverLocalFile.absolutePath)
Locale locale = getLocale()
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=" + locale.country);
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
options.addArguments("--user-data-dir=/data");
DesiredCapabilities jsCapabilities = DesiredCapabilities.chrome()
Map<String, Object> prefs = new HashMap<>()
prefs.put("intl.accept_languages", locale.toLanguageTag())
options.setExperimentalOption("prefs", prefs)
jsCapabilities.setCapability(ChromeOptions.CAPABILITY, options)
ChromeDriver chromeDriver = new ChromeDriver(options)
chromeDriver.manage().window().setSize(getDimension())
return chromeDriver
To obtain verbose logs from ChromeDriver we can configure the logfile and the type_of_logging as follows:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
System.setProperty("webdriver.chrome.logfile", "C:\\Utility\\BrowserDrivers\\chromedriver.log");
System.setProperty("webdriver.chrome.verboseLogging", "true");
Update :
I can see from your code you have provided :
System.setProperty('webdriver.chrome.driver', chromeDriverLocalFile.absolutePath)
In a similar fashion try to provide :
System.setProperty('webdriver.chrome.logfile', chromeDriverLocalFile.absolutePath);
System.setProperty('webdriver.chrome.verboseLogging', boolean);
From the documentation of ChromeDriver - WebDriver for Chrome
System.setProperty("webdriver.chrome.logfile", "Your path");
System.setProperty("webdriver.chrome.verboseLogging", "true");

How do you automatically open the Chrome Devtools tab within Selenium (C#)?

I see that there's a relatively new option to open Chrome with Devtools open from the command line, which I have gotten to work from my Windows 8.1 command line using a call like this:
c:\Program Files (x86)\Google\Chrome\Application>"chrome.exe" --auto-open-devtools-for-tabs
When I try to add this option on the same box when creating my ChromeDriver in Selenium (in C#), however, the option seems to be ignored.
var options = new ChromeOptions();
options.AddArgument("auto-open-devtools-for-tabs");
string executingAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
string driverPath = Path.Combine(Path.GetDirectoryName(executingAssembly), "ChromeWebDriver");
_driver = new ChromeDriver(driverPath, options);
I've tried a few variations on theme to make sure options are working at all, including...
var options = new ChromeOptions();
options.AddArguments(new[] { "start-maximized", "auto-open-devtools-for-tabs"});
... and...
var options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddArgument("auto-open-devtools-for-tabs");
... and...
var options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddExcludedArgument("auto-open-devtools-for-tabs");
... as well as setting those with -- in front of each option string. All I get from any of those are maximized windows.
I get the feeling the auto-open-devtools-for-tabs argument's not supported by Selenium's Chrome Web Driver, but I'm not sure why that wouldn't support the same set of options as the "full" app.
Anyone have this option working with Selenium in C#, or know why it shouldn't be working in this case?
This is not unlike this question, but here I'm asking specifically about the auto-open-devtools-for-tabs option with C#. That asker claims not to have had any luck with options, and was asking how to open devtools from "within" Selenium, looking for a method explicitly before this option existed.
I've tried this with in VS 2017, Selenium v3.12.1#, Firefox v60.0.2, Chrome v66, Nunit v3.10.1, Gecko Driver v20.1, and Chrome driver v2.4 (all using C#).
I tried to search for Firefox but did not have any success. I did find a solution for Chrome v66.
Please provide profile like this: options.AddArguments("--auto-open-devtools-for-tabs");
This is a complete chrome driver implementation:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--start-maximized");
options.AddArguments("disable-infobars");
options.AddArguments("--disable-notifications");
options.AddArguments("--auto-open-devtools-for-tabs");
driver = new ChromeDriver(DrivePath, options, TimeSpan.FromSeconds(100));
See also this post: "List of Chromium Command Line Switches"
Below commands are NOT working, this is issue with Geckodriver so Gecko team has to provide some solution or fix for that:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.F12);
Actions action = new Actions(driver); action.SendKeys(Keys.F12); action.Perform();
Actions action = new Actions(driver); action .KeyDown(Keys.Control)
.SendKeys(Keys.F12).KeyUp(Keys.Control).Perform();
Actions action = new Actions(driver); action.SendKeys(Keys.F12); action.Click();
Following the thread on SO-12212504 and leading from the selected answer.
One of the solution to this would be pressing F-12 [Key F12 Documentation] key using :
// without an element
new Actions(driver).SendKeys(Keys.F12).Perform();
// send keys to body
new Actions(driver).SendKeys(driver.FindElement(By.XPath("//body")), Keys.F12).Perform();
On the other side could you try and use AddUserProfilePreference from amongst the ChromeOptions Methods :
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("auto-open-devtools-for-tabs", "true");
Note : I am not very sure about the parameter name, but I hope you can find something corresponding here.
Edit : Some more attempts using keyboard shortcuts for the same -
Windows : [F12 or Ctrl + Shift + I]
String openDevTools = Keys.chord(Keys.CONTROL, Keys.SHIFT, "I");
driver.FindElement(By.XPath("//body")).SendKeys(openDevTools).Perform();
Mac : [Cmd + Opt + I]
String openDevTools = Keys.chord(Keys.COMMAND, Keys.ALT, "I");
driver.FindElement(By.XPath("//body")).SendKeys(openDevTools).Perform();
Ruby: must have installed latest selenium-webdriver (3.7.0) gem
options1 = Selenium::WebDriver::Chrome::Options.new
options1.add_argument('--auto-open-devtools-for-tabs')
driver = Selenium::WebDriver.for :chrome, options: options1
driver.get("https://stackoverflow.com")
I think the issue is with your options being declared as a var and not ChromeOptions, this code open google.com with dev tools open
public static void Scraps()
{
//Declare options variable and set dev tools argument
ChromeOptions co = new ChromeOptions();
co.AddArguments("--auto-open-devtools-for-tabs");
//Initiate driver instance and go to google.com
IWebDriver driver = new ChromeDriver(co);
driver.Url = "https://www.google.com/";
}

Unable to switch to new tab in Chrome in Selenium Webdriver

I have a issue while switching to new tab in chrome, by following some links on internet i came to know it is the issue with Chrome Driver, but i have latest chrome driver(2.21) and updated chrome browser(version 50).
Issue : unknown error: DevTools returned unknown type:webview
(After debugging, i get to know the issue is coming from driver.getWindowHandles();)
Pls help!!!
You should post the code that is giving error, nevertheless, below is the code that can be used to switch between chrome tabs:
System.setProperty("webdriver.chrome.driver",
"D:\\path\\to\\chrome\\exe\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String currWindow = driver.getWindowHandle();
driver.get("https://www.google.com");
Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();
// Open new tab
Set<String windowsList = driver.getWindowHandles();
for (String str : windowsList) {
if (!str.equals(currWindow)) {
driver.switchTo().window(str);
}
}
driver.get("https://www.facebook.com"); // open facebook

adding extension to chrome using chromeoptions doesnot work in selenium

I am a have a chrome extension developed, which I would like to have added to chrome.
When loaded the extension using the below code in selenium
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", prefs);
String CurrentDir = System.getProperty("user.dir");
String pluginDir = CurrentDir + "\\src\\main\\resources\\extensions\\newPlugin\\Chrome\\";
chromeOptions.addArguments("--load-extension=", pluginDir);
WebDriver driver = new ChromeDriver(chromeOptions);
Is there something I am missing? Please let me know

Unable to Launch Google Chrome using default/custom profile in Selenium WebDriver that undergo http basic authentication

Following methods are not working properly.
Since GRID is used, capability is set as null here.
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
DesiredCapabilities capability=null;
Method 1:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--user-data-dir=C:/Users /username/AppData/Local/Google/Chrome/User Data/Default"));
driver = new ChromeDriver(capabilities);
Method 2:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
String chromeProfile = "C:/Users/username/AppData/Local/Google/Chrome/Application /chrome.exe";
ArrayList<String> switches = new ArrayList<String>();
switches.add("C:/Users/username/AppData/Local/Google/Chrome/User Data/Default" + chromeProfile);
capabilities.setCapability("chrome.switches", switches);
driver = new ChromeDriver(capabilities);
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);
If you face such error:
org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally
Then try to create a new Chrome profile and execute tests.
Copy the folder, 'User Data'
Paste & Rename the folder on the same location. e.g., New User
Now, locate the directory, C:/Users/user_name/AppData/Local/Google/Chrome/New User
If you like to test the profile, then bookmark some of the sites & observe them on next run.
1 Set the chromedriver property in starting the node. My approach:
java -jar selenium-server-standalone-2.31.0.jar -role node -hub http://localhost:4444/grid/register -maxSession 15 -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=15 -Dwebdriver.chrome.driver=lib\chromedriver.exe
2 Inside the code my approach:
capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), capabilities);
3 loading the HTTP basic auth page:
String username = "Pavel";
String password = "Omgtoosecrettotellyou";
driver.get("http://" + username + ":" + password + "#" +"your-site.com");