0

When I go to this site: https://www.flipkart.com/

I'm prompted to enter a username and password but I am unable to use the driver to send keys to either input fields and I also can't use the click method on the x button to skip logging in. I tried using By.xpath and By.className to interact with the WebElements but it won't work. Here's my code to show what I've tried.

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.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

class Test{

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

            //I don't think the pop up is a frame. This doesn't work.
            driver.switchTo().frame(1);
            //className of the 'X' button. Doesn't work
            driver.findElement(By.className("_2AkmmA _29YdH8")).click();
            //two xpaths of the 'X' button. Doesn't work
            driver.findElement(By.xpath("//button[@innertext='✕']")).click();
            driver.findElement(By.xpath("/html/body/div[2]/div/div/button")).click();
            //trying to send text to the email field. Both className and xpath doesn't work
            driver.findElement(By.className("_2zrpKA _1dBPDZ")).sendKeys("test");
            driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div[2]/div/form/div[1]/input")).sendKeys("test1");


        }
    }
Ramaster
  • 1
  • 1
  • Can you post the error which you are facing? If there is no error being face, please add explicit waits in order to wait for the element to be intractable before calling `sendKeys` method. – PRERNA PAL Jan 08 '20 at 05:13
  • Remove this line 'driver.switchTo().frame(1);' , and wait for element be clickable. Reference: https://stackoverflow.com/questions/38327049/check-if-element-is-clickable-in-selenium-java – Yun Jan 08 '20 at 08:40

2 Answers2

0

Try using relative XPATHs to identify elements. In your query, you can choose if any of the below XPATHs work.

//*[text() = 'Enter Email/Mobile number']//..//../input
//*[contains(text(),'Enter Email')]//..//../input
(//*[text() = 'Enter Email/Mobile number']//preceding::input)[last()]

You can learn more about XPATHs at Dev.to

Community
  • 1
  • 1
Naveen
  • 770
  • 10
  • 22
0

Maybe try this:

WebElement form = driver.findElement(By.className("Km0IJL"));
List<WebElement> inputs = form.findElements(By.tagName("input"));

inputs.get(0).sendKeys(username);
inputs.get(1).sendKeys(password);

When it comes to the 'x' button try one of these:

By.xpath("/html/body/div[2]/div/div/button");
By.cssSelector("body > div.mCRfo9 > div > div > button");

In general, as a tip, often try to locate some parent element before and then try to locate an element from it's scope.