Unable to find element in webdriver - selenium

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 ")

Related

Login fail using Selenium Java

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.

How to deal with canvas using Selenium?

I try to test this website https://classpad.net/classpad/use-as-guest using Selenium. I do not know how to make the form to select visible. Please see pictures below for more detail.
[Before I click on blank area]
[After I click]
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DriverScript {
public static WebDriver driver;
public static WebElement element;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.navigate().to("http://34.201.210.27/classpad");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.findElement(By.xpath("/html/body/div/div[2]/form/a[2]")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.className("scratchpaper-detail")));
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("$('.scratchpaper-detail .stickyContainer .inputForm').show()");
String element = driver.getTitle();
System.out.println(element);
}
}

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"))));

Webdriver Script is working fine in all other's browsers but not working in IE11

As my webdriver script is working well in Chrome, Firefox, Opera, Safari. But when I run this script in IE11 than it only opens the webpage after that it can't do any other functionality.
Please Help me and tell me what's wrong with it. I am using IE11 webdriver 64 bit. I already set the path for it in ENVIRONMENT VARIABLE.
As I am new to it. That's why I tried with this simple code.
My Script:
package facebook;
import java.util.concurrent.TimeUnit;
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.ie.InternetExplorerDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Pratice {
public static WebDriver driver = null;
public static void main(String[] args) throws Exception
{
//driver = new FirefoxDriver();
//driver = new ChromeDriver();
//driver = new InternetExplorerDriver();
driver = new SafariDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.facebook.com/");
//WebDriverWait wait1 = new WebDriverWait(driver, 10);
//wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(text(),'Create a Page')]")));
driver.findElement(By.xpath("//input[#id='u_0_n']")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(text(),'Sign up for Facebook')]")));
String a = driver.getTitle();
System.out.println(a);
Thread.sleep(5000);
driver.close();
}
}
Same thing happened with me,I set a registry entry,please see here.