URL not typing in firefox - selenium

I am facing a problem when executing Selenium in Eclipse.
I am using Eclipse Luna, Mozilla Firefox 41 and Selenium 2.48.2. When I try to run this below code, URL is not typing in Firefox.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class first {
public static void main(String args[])
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}

Related

Error when running code as java application

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SELintroduction {
public static void main(String[] args) {
//Invoking Browser
//Chrome - ChromeDriver .exten-.Methods close get
//Firefox- Firefoxdriver ->methods close get
//safari SaariDrier ->methods close get
//WebDriver close get
//WebDriver methods + class methods
// chromedriver.exe->Chrome browser
System.setProperty("webdriver.chrome.driver","C:\\Users\\user\\Downloads\\chromedriver_win32.exe");
//webdriver.chrome.driver->value of path
WebDriver driver = new ChromeDriver();
driver.get("http://rahulshettyacademy.com");
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
what is the wrong here coding shows error?

WebElement Click() showing NullPointerException in Selenium

I was trying Selenium automation testing in https://www.yatra.com/etw-desktop/ . My objective was to click an Image Button named 'Asia' which will redirect to another page (Images attached). I copied the full XPath and tried but I am getting a NullPointerException. Please give some suggestions since I didn't find anything wrong with my code.
package com.stackroute.SeleniumProject;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
/**
* JUnit project.
*/
public class Yatra {
public static WebDriver driver = null;
#FindBy(xpath = "/html/body/my-app//app-drawer-layout/app-header-layout/iron-pages/my-home//div/div/div/div/paper-material[2]/div[1]/div/a[2]/div[4]")
WebElement Asia;
#BeforeClass
public static void setup() {
String chromePath = System.getProperty("user.dir") + "/lib/chromedriver.exe";// directory of chrome driver
System.setProperty("webdriver.chrome.driver", chromePath);
driver = new ChromeDriver();
}
#AfterClass
public static void close() throws InterruptedException {
driver.close();
}
#Test
public void test1() throws InterruptedException {
driver.manage().window().maximize();
driver.get("https://www.yatra.com/etw-desktop/");
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
// the error is in the below line Asia.click()
Asia.click();
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
Thread.sleep(2000);
Assert.assertEquals("page not found", "https://www.yatra.com/etw-desktop/city-list", driver.getCurrentUrl());
}
}
Image showing the element to be clicked
Image of web page after click operation
You are using PageFactory(from page object model), so you will need to init() the webelements.
For this you will need to import
org.openqa.selenium.support.PageFactory;
An before starting the test, you will need to initialise the elements:
PageFactory.initElements(driver, this) // where you pass the driver and this class to know which webelements to start.
You can take a look here to understand the approach and another POM framework easier to understand TUTORIAL

Unable to launch HtmlUnitdriver through Selenium 3.4.0

I am tried to run the HtmlUnitDriver with selenium 3.4 and chrome Version 64.0.3282.119 (Official Build) (32-bit).
My code is:
package eclipse;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.gargoylesoftware.htmlunit.BrowserVersion;
public class Unit
{
WebDriver driver;
public static void main(String[] args){
WebDriver driver;
driver = new HtmlUnitDriver(BrowserVersion.CHROME);
driver.get("http://www.google.com");
System.out.println(driver.getTitle());
}
}
Use this code to Initialize headless browser :
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME ,true);
driver.get("your web URL");
To invoke HtmlUnitDriver with Selenium v3.4.0 you don't need ChromeDriver or Chrome Browser Client instead you can use the org.openqa.selenium.htmlunit.HtmlUnitDriver module following the solution below :
Code Block :
package HtmlUnitDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class htmlUnitDriver {
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
System.out.println("Invoking Facebook through HtmlUnitDriver");
System.out.println(driver.getTitle());
}
}
Console Output :
Invoking Facebook through HtmlUnitDriver
Facebook – log in or sign up
Note : Ensure that HtmlUnitDriver() is not resolved from :
com.gargoylesoftware.htmlunit.BrowserVersion;

I'm not getting title of the page,i tried in firefox as well as in chrome

I'm not getting title of the page,i tried in firefox as well as in chrome.
This is my package
package begin;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Title {
WebDriver driver;
public void tite()
{
driver=new FirefoxDriver();
System.setProperty("webdriver.firefox.driver","C:/selenium-java-3.0.0-beta3/Latest selenium/geckodriver.exe");
driver.get("http://newtours.demoaut.com/");
String titleofthepage=driver.getTitle();
System.out.println(titleofthepage);
}
public static void main(String[] args)
{
Title obj1=new Title();
obj1.tite();
}
}
Need to add wait attributes to driver element.
After creating driver add implicit wait
System.setProperty("webdriver.firefox.driver","geckodriverpath");
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
You are finding page title immediately after launching web page. Here it will wait 30 secs before finding any element to web page.
Set the property before driver initialization So your code should be :
package begin;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Title {
WebDriver driver;
public void tite()
{
System.setProperty("webdriver.firefox.driver","C:/selenium-java-3.0.0-beta3/Latest selenium/geckodriver.exe");
driver=new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
String titleofthepage=driver.getTitle();
System.out.println(titleofthepage);
}
public static void main(String[] args)
{
Title obj1=new Title();
obj1.tite();
}
}

Launching URL using Selenium gecko driver

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CheckoutFlow {
public static void main(String[] args) {
// TODO Auto-generated method s
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("http://google.com");
}
}
While launching the URL - from Java code below error is coming
"Server not Found"
Your code is perfectly fine. This might be the issue with Firefox version,
firefox version(I'm using) 52.0.1.
If still it is not working then try to switch to chrome instead of firefox.
Chrome Code :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class CheckoutFlow {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
driver.quit();
}
}