Unable to find Element using selenium(Htmlunitdriver) - selenium

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\EclipseWorkSpace\\Seleniumdownloads\\chrome\\chromedriver_win32\\chromedriver.exe");
//WebDriver driver = new ChromeDriver();
// Selenium 3.0 we need to down load Html unit driver.
// URL : https://github.com/SeleniumHQ/htmlunit-driver/releases
WebDriver driver = new HtmlUnitDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.freecrm.com/");
driver.findElement(By.xpath("//span[contains(text(),'Log In')]")).click();
System.out.println(" Before login" + driver.getTitle());
Thread.sleep(10000);
WebElement email = driver.findElement(By.xpath("//input[#name='email']"));
email.sendKeys("test32145#hotmail.com");
// driver.findElement(By.xpath("//input[#name='email']")).sendKeys("test2135#hotmail.com");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("SWeet$123");
driver.findElement(By.xpath("//div[#class='ui fluid large blue submit button']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[contains(text(),'Contacts')]")).click();
System.out.println(" after login" + driver.getTitle());
Thread.sleep(2000);
// read.sleep(millis);
driver.close();
}

Related

No such element in selenium java

public class listenerdemo {
#Test
public void login() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
System.out.println(" The Page Title...." + driver.getTitle());
System.out.println("The Page URL...." + driver.getCurrentUrl());
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(500));
wait.until(ExpectedConditions. visibilityOfAllElementsLocatedBy (By.xpath("//*[#id='webklipper-publisher-widget-container-notification-close-div']")));
driver.close();
}
}
popup

Unable to Launch browser

public class LaunchBrowser {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
System.setProperty("webdriver.gecko.driver", "/Users/spectra/Drivers/geckodriver");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
driver.get("https://www.seleniumhq.org");
}
}
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.simplilearn.day2.oops.LaunchBrowser.main(LaunchBrowser.java:16)
setProperty before driver initialization which should be like
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "/Users/spectra/Drivers/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
driver.get("https://www.seleniumhq.org");
}

Null pointer exception on testng

I have written the below code and getting the java null pointer exception.
please help what is the problem.
public class testngbasics {
WebDriver driver;
#BeforeMethod
public void setbrowser() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
driver.get("http://demo.guru99.com/v4/");
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
}
#Test
public void login1() {
driver.findElement(By.xpath("//input[#name='uid']")).sendKeys("mngr212595");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("EgebYpy");
driver.findElement(By.xpath("//input[#value='LOGIN']")).click();
System.out.println(driver.getTitle());
}
#AfterMethod
public void closebrowser()
{
driver.close();
}
}
I am getting the error:
java.lang.NullPointerException it is only going to #beforemethod
annotation and not going to #test and #aftermethod
As you have already defined a global instance of WebDriver as driver in the line:
WebDriver driver;
You don't have to create another instance of WebDriver again as in:
WebDriver driver= new ChromeDriver();
And you need to replace this line as:
driver = new ChromeDriver();

How do I select package using selenium webdriver

public class chrome {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://neontv.co.nz");
Thread.sleep(2000);
driver.findElement(
By.xpath("//*[#class='nv-nav-freetrial-2 nv-nav-item']"))
.click();
Thread.sleep(200);
driver.findElement(
By.xpath("//ul[#id='yui_patched_v3_11_0_2_1529643447183_22']"))
.click();
Thread.sleep(2000);
driver.quit();
}
}
I am unable to select package on next page.. I tried changing the xpath but it didnt help.. is there any other way to select this. Could any one help please
Using Action classes, I was able to resolve this
driver.switchTo().defaultContent();
Actions action = new Actions(driver);
action.moveToElement(btnEnjoy).click().build().perform();

New Tab is not opening in Selenium

New Tab is not opening in Selenium
import org.testng.annotations.Test;
public class SimpleTest {
#Test
public void TestMethod() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\22November2017\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/");
Thread.sleep(3000);
WebElement element=driver.findElement(By.linkText("Gmail"));
Thread.sleep(3000);
element.sendKeys(Keys.CONTROL,"t");
}
}
Please help me find the error
Here is working code..it opens Gmail into new tab and switch to it.
import org.testng.annotations.Test;
public class SimpleTest {
#Test
public void TestMethod() throws InterruptedException{
System.setProperty("webdriver.gecko.driver","C:\\22November2017\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/");
Thread.sleep(3000);
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("Gmail")).sendKeys(selectLinkOpeninNewTab);
Thread.sleep(3000);
ArrayList<String> tab = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tab.get(1));
}
}
To open a New Tab with the linkText("Gmail") you can use the following code block :
String URL="http://www.google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(URL);
System.out.println("Page Title is : "+driver.getTitle());
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();
open the link in new Tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("http://www.google.com/")).sendKeys(selectLinkOpeninNewTab);
You can use JavaScriptExecutor to do it
Javascriptexecutor js = (Javascriptexecutor)driver;
js.executescript("var win = window.open('"+ YourURLHere + "', '_blank');win.focus(); ");
Above code will open new tab and navigate to the URL provided and focus on the new tab.