0

I am trying to learn selenium by testing them on different websites. In this process, I am trying to work with Flipkart website. In this, I would like to give puma is search bar and trying to click one of the resultant items. But I am not able to do that using below-mentioned code. Could anyone help in solving it?

Secondly, If we click on any item, it is redirected to new-tab. How to access the new-tab elements using the same script?

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
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 AutomationTesting {
    public static void main(String[] args) {

        System.setProperty("webdriver.gecko.driver","/Users/xxxx/eclipse-workspace/seleniumTesting/lib/geckoDriver/geckodriver");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.de");
        driver.findElement(By.id("lst-ib")).sendKeys("flipkart");
        driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
        WebDriverWait wait = new WebDriverWait(driver, 20); 
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Flipkart")));
        driver.findElement(By.partialLinkText("Flipkart")).click();
        driver.findElement(By.cssSelector("._3Njdz7 [class = '_2AkmmA _29YdH8']")).click();
        driver.findElement(By.xpath("//input[@class = 'LM6RPg']")).click();
        driver.findElement(By.xpath("//input[@class = 'LM6RPg']")).sendKeys("Puma");
        driver.findElement(By.xpath("//button[@class = 'vh79eN']")).click();
        driver.findElement(By.xpath("//a[@title='Puma Men Black Wallet' and @class= '_1Nyybr _30XEf0']")).click();
    }

}
Mallika M
  • 1
  • 1
  • which lines are giving you trouble? To my knowledge, I believe you can go to different tabs the same way you can go to a different window: `SwitchToWindow("nameOfTab");` – Mangohero1 Dec 29 '17 at 23:25
  • Unclear what do you ask. – Roman C Dec 29 '17 at 23:25
  • or [here's a link for how to switch tabs](https://stackoverflow.com/questions/12729265/switch-tabs-using-selenium-webdriver-with-java) since i'm not too familiar with Java – Mangohero1 Dec 29 '17 at 23:29
  • Question 1: During the execution of the last line in the above code I am getting the following error. Unable to locate element: //a[@title='Puma Men Black Wallet' and @class= '_1Nyybr _30XEf0'] Question 2: If that last command works then it is redirected to the new tab. So how to access the elements from the new-tab? – Mallika M Dec 30 '17 at 08:08
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – undetected Selenium Jan 02 '18 at 10:38

4 Answers4

2

You need to use the window switchTo feature.

String mainWindowHandle = driver.getWindowHandle();
ArrayList<String> wins = driver.getWindowHandles();
// You can use a for loop here, or get the assumed second window directly
driver.switchTo().window(wins.get(1));
// Test some things, then switch back
driver.close();
driver.switchTo().window(mainWindowHandle);

See http://www.seleniumhq.org/docs/03_webdriver.jsp#moving-between-windows-and-frames

Damian Jansen
  • 236
  • 1
  • 4
  • Thank you Damian Jansen, before using your code, it should execute the last line of my code where I am getting the following error. Unable to locate element: //a[@title='Puma Men Black Wallet' and @class= '_1Nyybr _30XEf0']. Please help me out? – Mallika M Dec 30 '17 at 08:13
  • your xpath got chnage that why ur getting error on last line – Ankur Singh Dec 30 '17 at 17:36
0

Use this code to reach upto puma and then select the option use below code

   public static void main(String[] args) throws InterruptedException, AWTException {

        System.setProperty("webdriver.chrome.driver","G:\\java programme\\SendkeysExample\\lib\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        driver.get("https://www.google.com");
        driver.findElement(By.id("lst-ib")).sendKeys("flipkart");
        driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);

        driver.findElement(By.linkText("Flipkart")).click();
        driver.findElement(By.className("LM6RPg")).sendKeys("Puma");

        Robot rb = new Robot();
        rb.keyPress(KeyEvent.VK_DOWN);
        rb.keyPress(KeyEvent.VK_DOWN);
        rb.keyPress(KeyEvent.VK_ENTER);

        rb.keyRelease(KeyEvent.VK_DOWN);
        rb.keyRelease(KeyEvent.VK_DOWN);
        rb.keyRelease(KeyEvent.VK_ENTER);

        Thread.sleep(2000);

        /*driver.findElement(By.xpath(".//*[@id='container']/div/header/div[1]/div/div/div/div[1]/form/ul/li[2]/a"));
        driver.findElement(By.className("icon-add-circle"))*/;
        driver.close(); 
    }

}
prat22
  • 378
  • 1
  • 3
  • 18
  • I am able to reach puma, that is not my issue. I am not able to select an item from the results of puma search. Please help me out in figuring it. – Mallika M Dec 30 '17 at 12:31
  • I have updated . please look into . you can fetch desired result using above code. notify me any problem – prat22 Jan 02 '18 at 11:25
0

You will have to inspect that entire frame. I would suggest instead of customising xpaths on your own try firebug and firepath add on of Firefox for getting xpath of entire elements. Inspect the entire frame within which the results are getting displayed then save it in some variable like below one:

List<WebElements> searchResults;  
searchResults=driver.findElements(By.xpath("Your xpath"));  

Then access elements of this list using index and then you can perform .click() action on the same. More on this link for capturing xpath using firebug and firepath

whatsinthename
  • 1,828
  • 20
  • 59
0

Question 1: During the execution of the last line in the above code I am getting the following error. Unable to locate element: //a[@title='Puma Men Black Wallet' and @class= '_1Nyybr _30XEf0']

--> Due to Lazy loading webelement not present in dom at the time when you are hiting click event on it. So to over come this you need to make the webElement on view and then hit the click event.

Please refer below code:-

    driver.get("https://www.google.de");
       driver.findElement(By.id("lst-ib")).sendKeys("flipkart");
       driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
       WebDriverWait wait = new WebDriverWait(driver,60); 
       wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Flipkart")));
       driver.findElement(By.partialLinkText("Flipkart")).click();

       try{
       driver.findElement(By.cssSelector("._3Njdz7 [class = '_2AkmmA _29YdH8']")).click();
       }catch(Exception e){

           System.out.println("No division");
       }

       driver.findElement(By.xpath("//input[@class = 'LM6RPg']")).click();
       driver.findElement(By.xpath("//input[@class = 'LM6RPg']")).sendKeys("Puma");
       driver.findElement(By.xpath("//button[@class = 'vh79eN']")).click();

      // Thread.sleep(3000);

       wait.until(ExpectedConditions.visibilityOf( driver.findElement(By.xpath("//a[@title='Puma Men Black Wallet']"))));

// getting element into view 
       ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("//*[@alt='Puma Men Black Wallet']"))); 

       Thread.sleep(2000);
       driver.findElement(By.xpath("//*[@alt='Puma Men Black Wallet']")).click();

Question 2: If that last command works then it is redirected to the new tab. So how to access the elements from the new-tab?

--> As suggested be @Damian Jansen add that code after last click event.

  String mainWindowHandle = driver.getWindowHandle();
    ArrayList<String> wins = driver.getWindowHandles();

   for(String win : wins ){

       driver.switchTo().window(win);

      // other operation 

       System.out.println(driver.getTitle());   

 }
// back to old window
   driver.switchTo().window(mainWindowHandle);

   System.out.println(driver.getTitle());

Hope this help you :)

Ankur Singh
  • 1,239
  • 1
  • 8
  • 18