I'm new to Selenium and in my code I'm trying to use my default profile from chrome with the browser that the selenium opens (without the cookies.)
Here is my code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class tryf {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\******\\Downloads\\chromedriver_win32\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\Users\\******\\AppData\\Local\\Google\\Chrome\\User Data");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
driver.manage().window().maximize();
//WebElement okStart;
//okStart = driver.findElement(By.id("close-instructions"));
//okStart.click();
}
}
My problam is that the code won't compile, with that eror
Error:(15, 16) java: no suitable method found for addArguments(java.lang.String)
method org.openqa.selenium.chrome.ChromeOptions.addArguments(java.lang.String...) is not applicable
(argument mismatch; java.lang.String cannot be converted to java.lang.String[])
method org.openqa.selenium.chrome.ChromeOptions.addArguments(java.util.List) is not applicable
(argument mismatch; java.lang.String cannot be converted to java.util.List)
Hope for help, Thanks :)
You are passing a string parameter in following statement.
options.addArguments("user-data-dir=C:\Users\******\AppData\Local\Google\Chrome\User Data");
The string parameter is not applicable as highlighted by the stacktrace.
method org.openqa.selenium.chrome.ChromeOptions.addArguments(java.util.List) is not applicable
(argument mismatch; java.lang.String cannot be converted to java.util.List)
You can look at the documentation to find which method meets your requirements.
The overloaded addArguments methods signature are below;
ChromeOptions addArguments(java.util.List<java.lang.String> arguments)
Adds additional command line arguments to be used when starting Chrome.
ChromeOptions addArguments(java.lang.String... arguments)
Related
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)
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\\");
I am trying to open google.com first then type "selenium testing".
I only wanted to use className for webdriver using eclipse but I am getting the following error.
Exception in thread "main"
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"class name","selector":"Tg7LZd"}
Command duration or timeout: 37 milliseconds
Here is my code:
package coreJava;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Training1 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.findElement(By.className("gLFyf")).sendKeys("selenium testing");
driver.findElement(By.className("Tg7LZd")).click();
}
}
How do I fix this?
This error message...
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"class name","selector":"Tg7LZd"}
...implies that the GeckoDriver was unable to find any element as per the Locator Strategy you have used.
Your main issue is the classNames you have used are based on JavaScript and are generated dynamically which we can't guess before they are generated.
As an alternative you can use the following solution:
package coreJava;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Training1 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement myElement = driver.findElement(By.name("q"));
myElement.sendKeys("selenium testing");
myElement.submit();
}
}
System.setProperty("webdriver.gecko.driver", "geckodriver");
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://google.com");
Thread.sleep(3);
driver.findElement(By.className("gsfi")).sendKeys("selenium testing");
Thread.sleep(3);
driver.findElement(By.className("sbqs_c")).click();
Thread.sleep(3);
driver.close();
This is working code
.
These will open the google chrome and then write "selenium testing" in search box and then search it using the class.
enter image description here
package demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FacebookRegistration {
public static void main(String[] args) {
WebDriver driver = new FireFoxDriver();
driver.get("http://www.facebook.com");
}
}
Have you seen the typo in your IDE? You've imported FirefoxDriver and try to instantiate FireFoxDriver
You have exactly 2 problems in your code as follows :
import :
As you have used import org.openqa.selenium.firefox.FirefoxDriver; on similar lines you have to use :
WebDriver driver = new FirefoxDriver();
System.setProperty() :
While you work with Selenium v3.x.x you have to download the latest geckodriver binary from this link, save it in your system and provide the absolute path of the geckodriver binary through System.setProperty() line as follows :
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
Observing following error while executing my test script. Can someone help me in identifying the cause of failure.
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"lid"}
Command duration or timeout: 20.45 seconds
Here is the snippet of my code. Please note that the element exist in same frame hence frame switch is not needed.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class LoginPage
{
#Test
public void testLoginFail()
{
WebDriver driver = new FirefoxDriver();
driver.get("https://www.zoho.com/crm/");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.findElement(By.linkText("LOGIN")).click();
//WebDriverWait wait = new WebDriverWait(driver,40);
//wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("lid"))));
driver.findElement(By.id("lid")).sendKeys("xyz#gmail.com");
HTML view of the element is:
<input name="lid" id="lid" class="input usrbx" value="" onkeypress="clearmsg()" type="email">
I have inspected the webpage "https://www.zoho.com/crm/lp/login.html" and i am able to see an iframe in it and the input text boxes are inside it. Below is the working code.
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\Selenium\\Web Drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.zoho.com/crm/");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.findElement(By.linkText("LOGIN")).click();
//Switch to the frame
driver.switchTo().frame(0);
driver.findElement(By.id("lid")).sendKeys("xyz#gmail.com");
driver.quit();
}
Hope this helps you. Thanks.
Here is the Answer to your Question:
A few words about the Solution:
While working with Selenium 3.4.0 with geckodriver v0.16.1 & Mozilla Firefox 53.0 you need to download the latest geckodriver from here and provide the absolute path of the geckodriver through System.setProperty.
Try to avoid implicitlyWait as per recent updates implicitlyWait may die in fire soon.
The error you are seeing NoSuchElementException says it all. The id of the element is not traceable.
The element with id lid is within an iframe, so you need to switch to frame first.
Here is your own code with some simple tweaks in it:
#Test
public void testLoginFail()
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.zoho.com/crm/");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.findElement(By.linkText("LOGIN")).click();
driver.switchTo().frame("zohoiam");
driver.findElement(By.xpath("//input[#id='lid']")).sendKeys("xyz#gmail.com");
}
Let me know if this Answers your Question.