org.openqa.selenium.InvalidArgumentException: Invalid argument - selenium

I'm new to this site and I would like to request your help and expert opinion on this issue.
I'm currently trying to run a simple code using Netbeans and Selenium, but everytime I run the code I get the error "Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir"
This is the code I'm trying to run, something weird is that I can successfully run the same code on a different computer.
package selenium.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumTest {
private static WebDriver driver = null;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Kevin\\Documents\\Selenium\\Driver\\chromedriver.exe");
driver = new ChromeDriver();
driver.get ("https:\\www.google.com");
}
}
Chrome Version: 77.0.3865.90
ChromeDriver Version: 77.0.3865.40
Netbeans: 8.2
Thank you very much for your help

Use ChromeOptions. You can specify a directory that's not your user's directory:
ChromeOptions co = new ChromeOptions();
co.addArguments("user-data-dir=C:\\Some\\valid\\data dir\\");

Related

Autosuggestion for driver is not working on Selenium on Eclipse

I am trying to create my first automation test and when I try to get autosuggestions for driver ( the get(string) one), nothing appears. Also multiple error appear. IS there anything, like other extensions, that I need to install or what might be the problem?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\test\\chromedriver.exe");
WebDriver driver = new Chromedriver();
driver.
}
}
The following errors appear:
Multiple markers at this line
The type org.openqa.selenium.WebDriver is not accessible
Must declare a named package because this compilation unit is associated to the named module
'Introduction'(for line 1)
The type org.openqa.selenium.chrome.ChromeDriver is not accessible (for line 2)
Multiple markers at this line
WebDriver cannot be resolved to a type
Chromedriver cannot be resolved to a type( for line 10)

MSEdge Driver not launching Brwoser using selenium

I am using selenium to automate MS Edge (chromium) browser. I have downloaded correct driver i.e v79.0.309.43 which is same for driver and browser.
but when I run code , it simply shows message that it is launching on multiple ports .See Screenshot below=
Can someone point out what is issue here?
Thanks,
Nilesh
I test with Microsoft Edge(Chromium) Beta version 79.0.309.43 and the same version of Microsoft Edge(Chromium) WebDriver and it works. You could refer to the code below and change the path to your owns:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeOptions;
public class Edgeauto {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "your\\path\\to\\edge\\webdriver\\msedgedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("C:\\Program Files (x86)\\Microsoft\\Edge Beta\\Application\\msedge.exe");
EdgeOptions edgeOptions = new EdgeOptions().merge(chromeOptions);
WebDriver driver = new ChromeDriver(edgeOptions);
driver.get("https://www.google.com/");
}
}
Also please remember to have the location of Edge Beta and msedgedriver.exe on your PATH.

How to execute Firefox developer console commands from Selenium?

I would like to execute Firefox's screenshot --fullpage command from inside a Selenium java script.
The command is documented in Take a full page screenshot with Firefox
Is it possible?
You can just take a screenshot from within your Java code. From this answer: https://stackoverflow.com/a/3423347/8020699
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
This guy suggests using a library called aShot to take full page screenshots. Here's the link to the aShot jar. Here's the code he gives:
import java.io.File;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class FullPageScreenshot {
public static void main(String args[]) throws Exception{
//Modify the path of the GeckoDriver in the below step based on your local system path
System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");
// Instantiation of driver object. To launch Firefox browser
WebDriver driver = new FirefoxDriver();
// To oepn URL "http://softwaretestingmaterial.com"
driver.get("http://www.softwaretestingmaterial.com");
Thread.sleep(2000);
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(),"PNG",new File("D:///FullPageScreenshot.png"));
}
}

Error in selenium webDriver while executing the normal test case

Here is my code for selenium webDriver
package com.pack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GoogleSearchTest {
public static void main(String...args) {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://google.com");
String appTitle = driver.getTitle();
System.out.println("Application title is :: "+appTitle);
driver.quit();
}
}
Below is the Error I am getting:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
FirefoxDriver cannot be resolved to a type
at com.pack.GoogleSearchTest.main(GoogleSearchTest.java:9)
I need to understand why I am getting that error.
Looks like you are trying to run the code that is already having compilation issues. You should be seeing a tab called "Problems", where it shows all/ any possible errors and warnings. Do you see anything in that tab? If so it will walk through the steps that you need to follow to overcome the issue.
The other option is to download a fresh copy of eclipse IDE and configure your selenium. Let me know which one worked for you.

Need help on Desiredcapabilities class

Hi I am learning selenium grid for the first time.I am unable to understand why Desired capabilities class is used to set browser name and platform details in a remote machine.
please let me know if there is any proper documentation on it apart from google-wiki page
Thanks
prathima
Not all remote machines (or sessions) support the features requested by a user. E.g. user may want to use Chrome on Windows but some machines are only running Firefox on Linux.
The desiredCapabilities is used to describe the features of a session requested by user. You can refer to this link and this link for more info.
- when user send request to remote machine to run a particular test on
a particular browser on a particular platform.
- It doesn't mean that all the remote machine will support all the
features requested by user such as (Particular browser on a
particular machine).
**Example:**
- If user request to execute a testcase on internet explorer on Linux
platform. It doesn't mean that always Linux machine has internet
explorer it would have firefox not internet Explorer(Because it is
open source).
- Linux machine doesn't support the features requested by a User. so
that, that point of time we will use DesiredCapabilites class to set
the platform , Browser Name and version of browser of Remote Machine
to execute the Test.
Example:
package grid;
import static org.junit.Assert.*;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class SimpleGridTest
{
WebDriver driver;
String baseUrl , nodeUrl;
#Before
public void setUp() throws Exception
{
WebDriver driver;
String baseUrl , nodeUrl;
baseUrl = "https://www.facebook.com";
nodeUrl = "http://192.168.10.21:5568/wd/hub";
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WIN8_1);
driver = new RemoteWebDriver(new URL(nodeUrl),capability);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);
}
#Test
public void test() throws InterruptedException
{
driver.manage().window().maximize();
driver.get("https://www.google.co.in");
driver.findElement(By.linkText("Gmail")).click();
driver.findElement(By.id("Email")).sendKeys("abc#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("1234");
driver.findElement(By.id("signIn")).click();
}
#After
public void tearDown() throws Exception
{
driver.quit();
}
}