Why did this happen? Chrome opens but the URL does not open with the code.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Id {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver\\chromedriver.exe"); //open browser
WebDriver driver = new ChromeDriver();
**driver.get("google.co.in"); //open URL**
driver.findElement(By.name("q")).sendKeys("Quadkast" + Keys.ENTER);
}
}
You need to give http/https protocol along with the url in the get() method
driver.get("http://www.google.com");
or
driver.get("https://google.com");
Related
I was trying to learn Selenium , i used flipkart site to automate , but when i redirect to flipkart i cant able to close a pop up
I used the 'x' button by its xpath & classname but its not working
[img]https://i.ibb.co/7bhDJYw/Screenshot-2022-11-29-at-10-31-19-AM.png[/img]
package ui;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Popup {
public static String browser = "Firefox";
public static FirefoxDriver driver;
public static void main(String args[]) throws InterruptedException {
if (browser.equals("Firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else if (browser.equals("Chrome")) {
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
} else if (browser.equals("Edge")) {
WebDriverManager.edgedriver().setup();
EdgeDriver driver = new EdgeDriver();
}
driver.manage().window().maximize();
driver.get("https://www.flipkart.com/");
// Thread.sleep(5000);
// driver.findElement(By.className("_2KpZ6l _2doB4z")).click();
Thread.sleep(2000);
// Alert alert = (Alert) driver.switchTo().alert();
// alert.dismiss();
}
}
this is the code i tried
Instead of doing this
driver.findElement(By.className("_2KpZ6l _2doB4z")).click();
do this
driver.findElement(By.xpath("//button[contains(text(),'✕')]")).click();
or
driver.findElement(By.cssSelector("._2KpZ6l._2doB4z")).click();
I am trying to run my application in headless mode using Chrome Browser, Selenium and Java. But it open a new chrome instance and start running the script in normal UI mode(can see the execution)
Is there anything wrong in code or browser compatibility.
OS - Windows 10,
Chrome Version - 85.0.4183.121
Below is my code -
package XYZ;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Test{
public static void main(String args[]) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\TestUser\\Desktop\\SeleniumWorkspace\\ABC\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
options.addArguments("window-size=1400,800");
options.addArguments("disable-gpu")
//options.addArguments("--headless", "--disable-gpu", "--window-size=1400,800","--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
System.out.println(driver.getCurrentUrl());
}
}
I can see you are missing "--" before passing the arguments in the code. Below is the correct code to use headless chrome while running your automated cases:
package XYZ;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Test{
public static void main(String args[]) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\TestUser\\Desktop\\SeleniumWorkspace\\ABC\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--window-size=1400,800");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
System.out.println(driver.getCurrentUrl());
}
}
For me this is working fine. I hope this solves your probelm.
I'm trying to launch firefox browser from eclipse using selenium as I'm learning selenium.
My tutor wrote the below code but when i'M trying the same code I get this exception-
Exception in thread "main" java.lang.IllegalStateException:
The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see
Link1. The latest version can be downloaded from
Link2
Code:
package appselenium1;
import org.openqa.selenium.firefox.FirefoxDriver;
public class A {
public static void main(String[] args) {
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
}
}
You are facing this exception, because you have not used gecko driver , which is required for launching and sending command in selenium.
You can download the latest version of gecko version from here
Try this :
package appselenium1;
import org.openqa.selenium.firefox.FirefoxDriver;
public class A {
static WebDriver driver ;
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Downloads\\geckodriver-v0.20.1-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
}
}
While you work with Selenium ver3.x, GeckoDriver ver0.21.0 and Firefox ver61.0.1, you need to download the latest GeckoDriver from mozilla/geckodriver and store it anywhere within your system. In your code you need to provide the absolute path of the GeckoDriver through the System.setProperty() line as follows:
package demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class A_Firefox
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "C:/path/to/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
System.out.println("Page Title is : "+driver.getTitle());
driver.quit();
}
}
Note: Replace the packagename (demo in this example) with your own package name.
I intend to perform some tests by using selenium with multiple web browsers. To distinguish between the different web drivers, I use the following line of code:
((RemoteWebDriver) driver).getCapabilities().getBrowserName();
This will return a String indicating the web browser that is used by the driver object. However, for my Opera WebDriver object this will give me the String 'chrome'. I have tried changing this by explicitly setting the browser name to 'opera' using DesiredCapabilities:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("opera");
WebDriver driver = new OperaDriver(capabilities);
Unfortunately, this does not fix my problem. How do I effectively change the web browser name?
Your basic requirement is to identify browser initialization if I'm right, which can be done by getting user-agent from your browser using JavascriptExecutor as following:
String userAgent = (String) ((JavascriptExecutor) driver).executeScript("return navigator.userAgent;");
//following is for identifying opera browser initialization
if(userAgent.contains("OPR/"){
System.out.println("Browser currently in use is Opera");
}
Similarly you can identify other browser initilisation by referring this link
Unfortunately , you would not be able to change the BrowserName.
What instead you can try is to create function for specifically handling multiple browsers: -
package multiBrowser;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.testng.annotations.Parameters;
public class MultiBrowserClass {
WebDriver driver;
#Test
#Parameters("browser")
public void multiBrowsers(String browserName) throws InterruptedException{
if(browserName.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.firefox.marionette","D:\\My Work\\Setup\\JAR\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
driver = new FirefoxDriver(myprofile);
}
if(browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "D:\\My Work\\Setup\\JAR\\driver\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver", "D:\\My Work\\Setup\\JAR\\driver\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if(browserName.equalsIgnoreCase("opera")){
System.setProperty("webdriver.opera.driver", "D:\\My Work\\Setup\\JAR\\driver\\operadriver.exe");
driver = new OperaDriver();
}
driver.manage().window().maximize();
driver.navigate().to("https://");
System.out.println(driver.getTitle());
driver.findElement(By.xpath("//div[#id='navbar-main']/ul/li[5]/a")).click();
driver.findElement(By.xpath("//div[#id='navbar-main']/ul/li[5]/ul/li/a")).click();
Thread.sleep(3000);
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys("abc#mm.kk");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("1qaz2wsx");
Thread.sleep(3000);
driver.findElement(By.xpath("//form[#id='loginform']/div[8]/button")).click();
Thread.sleep(5000);
if(driver.getPageSource().contains("Welcome abc#mm.kk")){
System.out.println("User Successfully logged in");
}else{
System.out.println("Username or password you entered is incorrect");
}
driver.quit();
}
}
=======
I am trying to open Chrome browser from Selenium webdriver but I'm failing to do so. At first I tried opening both Chrome and Firefox from the same program. The Firefox browser works perfectly fine, while I got error related to ChromeDriver exe file being not present. I downloaded the ChromeDriver file and added that to the External Jars and also called it using the System.setProperty( method.
Here is the original code:
package test.selenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Selenium_test {
public static void main(String[] args) {
FirefoxDriver dr1=new FirefoxDriver();
FirefoxDriver dr2=new FirefoxDriver();
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
ChromeDriver dr3=new ChromeDriver();
ChromeDriver dr4=new ChromeDriver();
dr1.get("http://google.com");
dr2.get("http://northeastraveller.com");
dr3.get("http://quora.com");
dr4.get("http://facebook.com");
// TODO Auto-generated method stub
}
}
I separated the Chrome part into a separate program named "Chrome_test", whose code is as follows
package test.selenium;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome_Test{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
ChromeDriver dr3=new ChromeDriver();
ChromeDriver dr4=new ChromeDriver();
dr3.get("http://quora.com");
dr4.get("http://facebook.com");
// TODO Auto-generated method stub
}
}
Now I'm getting the following error :
Error: Could not find or load main class test.selenium.Chrome_Test
I checked the classpath variables and all seems to be at place. What am I missing here?
You better place two backward slashes like:
System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver.exe");
It will work.
I wrote code which downloads and installs the latest ChromeDriver automatically to the project root directory if none has been found. That way you can receive a ChromeDriver instance without actually worrying about the chromedriver.exe file. Feel free to adjust it to your needs. You still need to include Selenium libraries in your project though. For my ChromeDriverFactory class below you also need Apache Commons IO and Zip4J.
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeDriverFactory
{
private static String chromeDriverRepository = "http://chromedriver.storage.googleapis.com/";
public static WebDriver getChromeDriver() throws MalformedURLException,
IOException, ZipException
{
String chromeDriverFileName = "chromedriver.exe";
File chromeDriverFile = new File(chromeDriverFileName);
if (!chromeDriverFile.exists())
{
installChromeDriver();
}
setChromeDriverProperty(chromeDriverFileName);
return new ChromeDriver();
}
private static void setChromeDriverProperty(String chromeDriverFileName)
{
System.setProperty("webdriver.chrome.driver", chromeDriverFileName);
}
private static void installChromeDriver() throws IOException,
MalformedURLException, ZipException
{
String newestVersion = getNewestVersion();
String targetFile = "chromedriver_win32.zip";
String downloadUrl = chromeDriverRepository + newestVersion + "/"
+ targetFile;
String downloadFileName = FilenameUtils.getName(downloadUrl);
File downloadFile = new File(downloadFileName);
String projectRootDirectory = System.getProperty("user.dir");
FileUtils.copyURLToFile(new URL(downloadUrl), downloadFile);
ZipFile zipFile = new ZipFile(downloadFile);
zipFile.extractAll(projectRootDirectory);
FileUtils.deleteQuietly(downloadFile);
}
private static String getNewestVersion() throws MalformedURLException,
IOException
{
String newestVersionUrl = chromeDriverRepository + "LATEST_RELEASE";
InputStream input = new URL(newestVersionUrl).openStream();
return IOUtils.toString(input);
}
}
Change the chrome driver properties line with backslashes (\) and it would work.
System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver.exe");