Pratiksha
Pratiksha

Reputation: 25

Unable to click the element. Error: no such element: Unable to locate element:

I am practicing on flipkart.com. i am unable to click the product after the search result. The xpath is correct. I tried using scroll function,visible, find by partial linktext, wait, sleep still i am unable to do. Please find the below code.

package Flipkart;

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

public class TC2 
{
    public static void main(String[] args) throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.flipkart.com/");
        driver.findElement(By.name("q")).sendKeys("Bag");
        driver.findElement(By.xpath("html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button")).click();
        driver.findElement(By.xpath(".//*[@id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]")).click();
    } 
}

  Error:
    Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 17575
    Only local connections are allowed.
    Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]"}
      (Session info: chrome=58.0.3029.81)
      (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 76 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
    System info: host: 'D90T0CQ1', ip: '192.168.162.83', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_102'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\SALUNK~1\AppData\Local\Temp\scoped_dir12552_16506}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.81, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
    Session ID: a8c3432c9b1e8cce3365db9407fc310f
    *** Element info: {Using=xpath, value=.//*[@id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
        at Flipkart.TC2.main(TC2.java:15)

Upvotes: 0

Views: 12154

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193208

Here is the Answer to your Question:

I don't see any such errors/issues in your code. A few words about the solution:

  1. Chrome browser sometimes doesn't opens maximized so you need to maximize the Chrome browser using ChromeOptions class.
  2. If the browser is not maximized certain elements doesn't appers within the Viewport and and may throw NoSuchElementException
  3. Add the argument to disable the infobars through ChromeOptions class.
  4. Your xpath looks vulnerable to me, try to construct logical xpath for the elements on the HTML DOM.
  5. Your own code works at my end with some simple tweaks which opens the Chrome Browser, navigates to the URL https://www.flipkart.com/, searches for Bag, clicks on Skybags Pixel Plus 03 30 L Backpack and Skybags Pixel Plus 03 30 L Backpack (Grey) is opened in a new tab.
  6. Here is the modified code block:

    System.setProperty("webdriver.chrome.driver","C:\\your_directory\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://www.flipkart.com/");
    driver.findElement(By.name("q")).sendKeys("Bag");
    driver.findElement(By.xpath("html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button")).click();
    driver.findElement(By.xpath(".//*[@id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]")).click();
    

Let me know if this Answers your Question.

Upvotes: 1

Chandra Shekhar
Chandra Shekhar

Reputation: 664

Final solution and I can't suggest more than this,

//openChromeBrowser();
    driver.get("https://www.flipkart.com/");
    driver.findElement(By.name("q")).sendKeys("Bag");
    driver.findElement(By.xpath("//form[@action='/search']/div/div[2]/button")).click();
    Thread.sleep(5000);
    driver.findElement(By.linkText("Puma PUMA Zipper Backpack 26 L Laptop Backpack")).click();

1> Give some wait time and it works 2> Use LinkText for locators for any of the item shown.

And follow what @Monika Singhal has said for quick and efficient results in Automation.

Upvotes: 0

Murthi
Murthi

Reputation: 5347

I got the same exception and added implicitly wait. It is working fine now. The code

System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") + "\\lib\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("https://www.flipkart.com/");
    driver.findElement(By.name("q")).sendKeys("Bag");
    driver.findElement(By.xpath("html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button")).click();
    driver.findElement(By.xpath("//*[@class='_2cLu-l']")).click();`

Upvotes: 0

Monika
Monika

Reputation: 732

I have tried the same lines of code which you have mentioned and things work for me. Maybe you should try again.

Please try to use small xpath:

Suggestion:

Replace Xpath: html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button with the different Xpath: //form[@action="/search"]/div/div[2]/button

You should use relative path instead of absolute path because if there are any changes made in the path of the element then that XPath gets failed.

Upvotes: 0

Related Questions