I use selenium webdriver to automate my test-cases.
My objective to execute headless browser using HtmlUnitDriver on a sample selenium script. Please find the script mentioned below:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Headless
{
public static void main(String[] args) throws InterruptedException
{
HtmlUnitDriver driver = new HtmlUnitDriver();
//driver.setJavascriptEnabled(true);
// WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.co.in/?gfe_rd=cr&ei=k36cVsa6OubI8Aec14bICQ&gws_rd=ssl");
/*WebDriverWait wait=new WebDriverWait(driver,120);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("sb_ifc0")));
*/
Thread.sleep(50000);
System.out.println("URL= "+driver.getCurrentUrl());
System.out.println("Page title is: " + driver.getTitle());
}
}
And the output is:
URL= about:blank
Page title is:
The output is working fine for FirefoxDriver()
Could anyone guide where I went wrong?
Related
I'm tring to send the login credentials within the username and password field https://www.vignanits.ac.in/server/moodle/login/index.php which needed to be automated using HtmlUnitDriver but facing NoSuchElementException.
Code trials:
package leela;
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.WebClient;
public class LeelaHtmlUnit {
public static void main(String[] args) throws InterruptedException {
HtmlUnitDriver driver = new HtmlUnitDriver(true);
driver.get("https://www.vignanits.ac.in/server/moodle/login/index.php");
System.out.println(driver.getCurrentUrl()+driver.getTitle());
driver.findElement(By.id("username")).sendKeys("xyz");
driver.findElement(By.id("password")).sendKeys("xyz");
driver.findElement(By.id("loginbtn")).click();
System.out.println(driver.getCurrentUrl()+driver.getTitle());
}
}
enter code here
Ideally, to use htmlunit-driver you need to download htmlunit-driver-2.42.0-jar-with-dependencies.jar the latest release as of today.
The below code block works perfect at my end:
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HtmlUnitDriverDemo {
public static void main(String[] args) {
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.get("https://www.vignanits.ac.in/server/moodle/login/index.php");
System.out.println(driver.getCurrentUrl()+driver.getTitle());
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("username")));
driver.findElement(By.id("password")).sendKeys("xyz");
driver.findElement(By.id("loginbtn")).click();
}
}
Console Output:
https://www.vignanits.ac.in/server/moodle/login/index.phpVIGNAN MOODLE: Log in to the site
Reference
You can find a detailed discussion on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element
I am trying to get the Error message printed in SalesForce website for entering incorrect username and password
package today;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Gmail {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe"); WebDriver mail=new ChromeDriver();
mail.get("https://login.salesforce.com/?locale=in");
mail.findElement(By.cssSelector("#username")).sendKeys("judinkp#gmail.com");
mail.findElement(By.xpath("//*[#id=\"password\"]")).sendKeys("23232");
mail.findElement(By.id("Login")).click();
System.out.println(mail.findElement(By.xpath("//*[#id='error']")).getText());
}
}
My Script runs till it clicks on Login but the Error message printed in website is not getting printed in my console , I am getting below error message
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='loginError']"}
(Session info: chrome=80.0.3987.149)
The Xpath is Browser given Xpath.
Website Link:https: //login.salesforce.com/?locale=in
Can you please try below solution:
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginScreen {
public static void main(String[] args) {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("https://login.salesforce.com/?locale=in");
driver.get("https://login.salesforce.com/?locale=in");
driver.findElement(By.cssSelector("#username")).sendKeys("judinkp#gmail.com");
driver.findElement(By.id("password")).sendKeys("23232");
driver.findElement(By.id("Login")).click();
WebDriverWait wait = new WebDriverWait(driver,20);
WebElement error= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("error")));
System.out.println(error.getText());
}
}
I had the same issue with CSS and Xpath locators.
Try with System.out.println(mail.findElement(By.id("error")).getText()); it should print out in the console.
package automation;
import org.junit.Test;
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.firefox.FirefoxDriver;
public class MainPage {
private final WebDriver driver;
public MainPage(WebDriver driver) {
this.driver = driver;
}
public MainPage loginAs(String username, String password) {
driver.get("URL");
driver.findElement(By.name("email")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.className("login")).click();
return new MainPage(driver);
}
public static void main(String[] args) {
System.setProperty("webdriver.genko.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
MainPage login = new MainPage(new ChromeDriver());
login.loginAs("qa#gmail.com", "123456");
}
}
I tried to test login function for the web site and wrote above code for it. However,
I couldnt find the error on it. Can someone help me to figure it out?
I see a few things wrong with your code:
System.setProperty("webdriver.genko.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
Should be (make sure the chromedriver is the correct file, the windows version usually has an .exe extension.)
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
You also should navigate the url of the page you want to test using
WebDriver driver = new ChromeDriver();
driver.get("http://[enter url here]")
Please write the "gecko driver" instead of "genko driver".
I am using Selenium PhantomJS WebDriver to crawl a DOM element.
Tried:
driver.findElementByXPath("//*[#class=\"_XWk\"]").getText()
It works well with Firefox WebDriver but doesn't works with PhantomJS WebDriver.
It shows error:
Caused by: org.openqa.selenium.NoSuchElementException: Error Message => 'Unable to find element with xpath '//*[#class="_XWk"]''
Below is the sample code which I tried.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class TestPhantomJs {
public static void testSearchReturnsResults() throws Exception {
// Create instance of PhantomJS driver
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
PhantomJSDriver driver = new PhantomJSDriver(capabilities);
// DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// FirefoxDriver driver = new FirefoxDriver(capabilities);
// Navigate to the page
driver.get("http://www.google.co.in");
driver.manage().window().setSize(new Dimension(1124, 850));
System.out.println(driver.getTitle());
// Input the search term into the search box
String searchTerm = "Prime Minister of India";
WebElement element = driver.findElement(By.cssSelector("input[name='q']"));
element.sendKeys(searchTerm);
element.sendKeys(Keys.RETURN);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
System.out.println(driver.findElementByXPath("//*[#class=\"_XWk\"]").getText());
System.out.println("\nFinish...");
driver.close();
}
public static void main(String[] args) throws Exception {
testSearchReturnsResults();
}
}
I am not able to make findElement method to work in the latest WebDriver (v2.52). Have imported all the required classes. Here is the error I'm receiving -
Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.By.findElement(Lorg/openqa/selenium/SearchContext;)Lorg/openqa/selenium/WebElement;
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at Flipkart_Test.main(Flipkart_Test.java:38)
Code that I'm running is -
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.remote.*;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import java.io.File;
import java.util.concurrent.TimeUnit;
public class Flipkart_Test {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
String appUrl = "http://flipkart.com";
driver.get(appUrl);
String expectedTitle = "Online Shopping India Mobile, Cameras, Lifestyle & more Online # Flipkart.com";
String actualTitle = driver.getTitle();
System.out.println(driver.getTitle());
// compare the expected title of the page with the actual title of the page and print the result
if (expectedTitle.equals(actualTitle))
{
System.out.println("Verification Successful - The correct title is displayed on the web page.");
}
else
{
System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
}
WebElement notificationLink = driver.findElement(By.id("notifications-link"));
notificationLink.click();
}
}
Remove
public static void main(String[] args) {
And use #Test - (Junit) to execute the test