Google search locate elements not working [duplicate] - selenium

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 NewGmail {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.findElement(By.id("identifierId")).sendKeys("cp8805");
//driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebDriverWait wait=new WebDriverWait(driver, 20);
driver.findElement(By.xpath("//span[#class='RveJvd snByac']")).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#class='whsOnd zHQkBf']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//span[#class='RveJvd snByac']")).click();
}
}
after mail id my password also get written in the id box option & the server redirect to to next password page. i want to ask what i will do so that my password would be entered only in password page.

Here is the working code block to login into your Gmail account through a valid set of credentials-
System.setProperty("webdriver.gecko.driver","C:\\your_directory\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement email_phone = driver.findElement(By.xpath("//input[#id='identifierId']"));
email_phone.sendKeys("your_email_phone");
driver.findElement(By.id("identifierNext")).click();
WebElement password = driver.findElement(By.xpath("//input[#name='password']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("your_password");
driver.findElement(By.id("passwordNext")).click();
Update(5-Jan-2020)
Optimizing the above code block and adding a couple of arguments you can use:
public class browserAppDemo
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.setExperimentalOption("useAutomationExtension", false);
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
WebDriver driver = new ChromeDriver(options);
driver.get("https://accounts.google.com/signin")
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='identifierId']"))).sendKeys("emailID");
driver.findElement(By.id("identifierNext")).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#name='password']"))).sendKeys("password");
driver.findElement(By.id("passwordNext")).click();
}
}

Related

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

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.