Login fail using Selenium Java - selenium

My webdriver is the last version, my code have Thread.sleep also, but I can't login to the page.
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
WebDriver driver;
#BeforeTest
public void navegador() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\chromedriver.exe");
Thread.sleep(1000);
driver = new ChromeDriver();
Thread.sleep(2000);
driver.manage().window().maximize();
Thread.sleep(3000);
}
#Test
public void f() throws InterruptedException {
driver.get("somewebpage");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Thread.sleep(3000);
driver.findElement(By.partialLinkText("LOGIN")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/div[1]/div[2]/form/div[1]/input")).sendKeys("myemail");
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/div[1]/div[2]/form/div[2]/input")).sendKeys("mypassword");
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/div[1]/div[2]/form/div[3]/div[2]/button")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
}
#AfterTest
public void cierre() {
driver.quit();
}
}
The webpage send me this message "These credentials do not match our records."
But when I do it manually, the page give me access to home.
I can't pass from the login page, Suggestions?

may be you can give try this check whether test works in debug mode.
If test is still not working in debug mode. then try to fetch values of both username and password field after sent by script and check whether its going correctly as manually.
Still its failing please share html code.

Related

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 find element in webdriver

I got this exception or error when I rum my script:
"Unable to locate element: *[name='password']"
I have tried with different locators but every time I get the same error.
Here is my script
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
public class TestGmail {
public static void main(String[] args){
System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.16.0-win32\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
//System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver_win32\\chromedriver.exe");
//WebDriver driver=new ChromeDriver();
driver.get("https://accounts.google.com/");
driver.findElement(By.id("identifierId")).sendKeys("myAddress");
driver.findElement(By.cssSelector("span.RveJvd.snByac")).click();
driver.findElement(By.name("password")).sendKeys("myPassword");
driver.findElement(By.className("RveJvd snByac")).click();
driver.close();
}
}
Here is the code block to locate the password field and send text into the password field on the url https://accounts.google.com/
package demo;
import java.util.concurrent.TimeUnit;
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class GMAIL_LOGIN_FIREFOX_CSS
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver","C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.cssSelector("#identifierId")).sendKeys("your_email");
driver.findElement(By.cssSelector(".ZFr60d.CeoRYc")).click();
WebElement password = driver.findElement(By.cssSelector("input[class='whsOnd zHQkBf'][type='password']"));
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("your_password");
}
}
Try this script it is workig fine:
WebDriver driver = new FirefoxDriver();
driver.get("https://accounts.google.com/");
driver.findElement(By.id("identifierId")).sendKeys("myAddress");
driver.findElement(By.cssSelector("span.RveJvd.snByac")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.name("password")).sendKeys("myPassword");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[#class='RveJvd snByac']")).click();
driver.close();
I would suggest you to use Explicit wait. Using implicit wait is a bad practice.
You can use below code something like below-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someid")));
Above code is in Java.
Use this code line for finding the "password" element and entering the password
Code is as follows:
driver.findElement(By.Xpath("html/body/div/div/div[2]/div[2]/form/div[2]/div/div/div[1]/div[1]/div/div[1]/div/div[1]/input")).sendKeys("Your password ")

Unable to click on login button of paytm. It will highlight but will not click. Below is my code

I am unable to click on login button of paytm. It will highlight but will not click.
The code works perfectly in Firefox. It's not able to click the link in Chrome browser. It's not showing any error or exception, it's just not able to click.
Below is my code:
driver.get("https://paytm.com/");
WebElement LoginLink= driver.findElement(By.xpath("//*[contains(text(),'Log In/Sign Up')]"));
Highlight.highLightElement(driver, LoginLink);
LoginLink.click();
public class LoginPage {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
String url="https://paytm.com/";
driver.get(url);
Thread.sleep(5000);
WebElement Login=(WebElement) driver.findElement(By.className("_3ac-"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", Login);
}
}
driver.manage().window().maximize();
driver.get("https://paytm.com/");
Thread.sleep(2000);
JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement Login_Btn= driver.findElement(By.xpath("//*[contains(text(),'Log In/Sign Up')]"));
je.executeScript("arguments[0].scrollIntoView(true);",Login_Btn);
Login_Btn.click();
Here is the Answer to your Question:
You can use the following code block to click on Log In/Sign Up button on https://paytm.com/ through Google Chrome 59.0:
Note: As a quick fix to your issue I have induced Thread.sleep(5000); which should be replaced by ExplicitWait i.e WebDriverWait
import java.util.concurrent.TimeUnit;
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 Q45102095_PAYTM
{
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://paytm.com/");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Thread.sleep(5000);
WebElement login_button = driver.findElement(By.xpath("//div[#id='app']//div[normalize-space(text()) = 'Log In/Sign Up']"));
login_button.click();
}
}
Let me know if this Answers your Question.

isEnabled() failed to find element

I am new to selenium. I trying to check whether button is enable or not through isEnabled(). But when I am running this program it generating a error as "Unable to locate element" of button.
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class test
{
static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "D:\\rakesh\\software\\selenium browser\\New folder\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://app.crossover.com/");
driver.manage().window().maximize();
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,5500)", "");
driver.findElement(By.linkText("Available Jobs")).click();
Boolean search_btn_ele = driver.findElement(By.xpath(".//*[#id='available-jobs']/div[2]/form/div/div[3]/button")).isEnabled();
if(search_btn_ele.FALSE)
{
System.out.println("Button is disable before giving search keys");
}
else
{
System.out.println("Button is enable before giving search keys");
}
WebElement search_txtfield_ele= driver.findElement(By.xpath(".//*[#id='available-jobs']/div[2]/form/div/div[1]/div/input"));
search_txtfield_ele.sendKeys("Chief");
}
}
Use WebDriverWait to wait for the element to be present:
new WebDriverWait(driver, TimeSpan.FromSeconds(45)).Until(ExpectedConditions.ElementIsVisible((By.Id("ctl00_ContentPlaceHolder1_drp85"))));

Unable to enter any data in Google page through Selenium

I am able to open a google page, but I am unable to enter any text in the Search bar. My execution stops only after opening the Google page.
Below is the code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirePath {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver Driver = new FirefoxDriver();
Driver.get("http://www.google.com");
//Driver.wait(50);
Driver.findElement(By.xpath(".//*[#id='gs_htif0']']")).sendKeys("some text");
//Driver.findElement(By.cssSelector(".gb_P")).click();
//Driver.findElement(By.xpath("html/body/nav/div/a[1]")).click();
}
}
try the following code, if you want to enter text in Search input field:
Driver.findElement(By.xpath("//input[#id='lst-ib']")).sendKeys("some text");
or
Driver.findElement(By.id("lst-ib")).sendKeys("some text");
You can use following code for this.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com/");
driver.findElement(By.id("lst-ib")).sendKeys("Some Text");
driver.findElement(By.id("_fZl")).click();