0

Below are three options I tried, but I'm not able to locate Login button. When I tried using seleniumIDE, I got this xpath=(//button[@type='submit'])[2]

But its not working in Webdriver. Can someone help me to resolve this?

xpath="//button[@type='submit']"
xpath="//button[@type='submit'][2]"
xpath="//button[@type='submit' and @class='_2AkmmA _1LctnI _7UHT_c']"
css=("button[type='submit'][2]")
  • 2
    Can you please share the HTML code and the error which you are getting. – shanila Jun 13 '18 at 12:05
2

Try using the following code for login functionality in Flipkart:

driver.findElement(By.linkText("Login & Signup")).click();
driver.findElement(By.className("_2zrpKA")).sendKeys(" YOUR USER NAME");
driver.findElement(By.class("_2zrpKA _3v41xv")).sendKeys("YOUR PASSWORD");
driver.findElement(By.className("_2AkmmA _1LctnI _7UHT_c")).submit();
2

I guess the "Login & Signup" button itself was not clicked and you would have got some error like, "Element is not clickable at point(x,y)..."

Try this code,

driver.findElementByXPath("//div[@class='_1jJkOg']/a").sendKeys(Keys.RETURN);
driver.findElementByXPath("(//input[@type='text'])[2]").sendKeys("username");
driver.findElementByXPath("//input[@type='password']").sendKeys("password");
driver.findElementByXPath("(//button[@type='submit'])[2]").click();
  • The above code will work, But could you please confirm how (driver.findElementByXPath("(//button[@type='submit'])[2]").click();)this will work in page factory concept? Because i used the same locator in page factory(xpath=(//button[@type='submit'])[2]), but it was not worked for me. – Techx Jun 22 '18 at 11:48
0

Thanks for the answers, i tried the below and it worked for me. I am not sure whether i followed a lengthy step

@FindBy(xpath="//button[@type='submit']")
List<WebElement> webElements;

for (int i = 0; i<=webElements.size(); i++){
            String str = webElements.get(i).getText();
             WebElement ele = webElements.get(i);
             //System.out.println(ele.getText());
             System.out.println(webElements.size());
            System.out.println(str);
             if (str.contains("Login"))
             {
                 System.out.println("PASS"); 
                 ele.click();
             }
0

For Me It's Working as:

package Webbasics;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FlipkartLogin {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\selenium\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.flipkart.com");
        driver.manage().window().maximize();


        driver.findElement(By.xpath("(//input[@type='text'])[2]")).sendKeys("bhargav@gmail.com");
        driver.findElement(By.xpath("//input[@type='password']")).sendKeys("Bhargav");
        driver.findElement(By.xpath("(//button[@type='submit'])[2]")).click();


    }

}
0
  1. xpath="//button[@type='submit']" => this will locate multiple submit type buttons on the webpage.

enter image description here

  1. //button@type='submit' => will locate nothing since there are no buttons of nth-child type 2 on the webpage:

enter image description here

Rather use the following xpath to uniquely select the login button the flipkart:
//button/span[contains(text(), 'Login')] OR
//button/span[contains(text(), 'Login')]/ancestor::button

  • I have provided an XPath that will correctly locate the login button on the Flipkart website. All the locators that user tried are either incorrect or will locate multiple elements on the HTML page. xpath I provided ill uniquely locate the login button. If needed I can write the complete code snippet. I just felt that won't be necessary since the user already has an idea of how to use the XPath to locate elements on a webpage. Let me know if I still need to update or delete my answer. – Kenil Fadia Aug 27 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.