0

enter image description hereI am not able to locate web Element Add to cart using Xpath as well as classname. Steps:trying to add product in cart /wishlist but button is not clickable. Can anyone please help me to resolve this issue.
=========================================================================== Code>>>

    package Test;
    import java.util.HashMap;
    import java.util.Map;
    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;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.Select;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class tedbak {

        public static void main(String[] args) throws InterruptedException {
            // TODO Auto-generated method stub
            WebDriver driver;

                System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver_win32\\chromedriver.exe");
                //System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
             Thread.sleep(2000);
            ChromeOptions options = new ChromeOptions();
            Map<String, Object> prefs = new HashMap<String, Object>();
            prefs.put("credentials_enable_service", false);
            prefs.put("password_manager_enabled", false); 
              options.setExperimentalOption("prefs", prefs);
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars");
            driver = new ChromeDriver(options);

                driver.get("http://www.tedbaker.com/");


                WebDriverWait wait = new WebDriverWait(driver, 40);
                wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")));
                        if(driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).isDisplayed())
                            {

                            driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).click();

                            WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]"));
                            Search_Item.click();
                            WebElement Seacrh_item_message=driver.findElement(By.id("search"));
                            WebElement Search_textbox=driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[1]/input"));

                            Search_textbox.sendKeys("Watches");
                            driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[2]/input")).click();
                            WebElement SearchResult_pagemessage=driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/h1"));
                            driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/div/div/span[1]")).click();

 WebElement Item_Tobuy = driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[2]/div[1]/article/div[2]/header/div/h4/a"));
                            JavascriptExecutor jse1= (JavascriptExecutor)driver;
                            jse1.executeScript("window.scrollBy(0,400)", "");
                            Item_Tobuy.click();
                                                         driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/ol/li[2]/span/span/select")).click();
                           Select dropdown1=new Select(driver.findElement(By.id("qty")));
                           dropdown1.selectByVisibleText("5");
                           driver.findElement(By.id("qty")).click();
                           driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/div[3]/div[2]/a/span[1]/span[1]")).click();
                                                                            }

    }
    }
===========================================================================    
    error>>>

    Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 17869
    Only local connections are allowed.
    Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/div[3]/div[2]/a/span[1]/span[1]"}
      (Session info: chrome=59.0.3071.86)
      (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 52 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
    System info: host: 'D90ZC6Q1', ip: '192.168.163.235', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_92'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41), userDataDir=C:\Users\DAHIBH~1\AppData\Local\Temp\scoped_dir8284_18567}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=59.0.3071.86, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
    Session ID: b48bc601ec61113cfada0c7c2f5f77b8
    *** Element info: {Using=xpath, value=html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/div[3]/div[2]/a/span[1]/span[1]}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        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 Test.tedbak.main(tedbak.java:70)
demouserabcd
  • 37
  • 1
  • 1
  • 10

3 Answers3

0

You need to change the last XPath in your code to "//*[@id='add_to_cart_form']/div[2]/div[2]/a/span[1]/span[1]"

driver.findElement(By.id("qty")).click();
driver.findElement(By.xpath("//*[@id='add_to_cart_form']/div[2]/div[2]/a/span[1]/span[1]")).click();
Buaban
  • 5,029
  • 1
  • 17
  • 33
0

try with the xpath //input[contains(@class,'add_to_cart')]

Murthi
  • 5,299
  • 1
  • 10
  • 15
  • thanks, customized path is working to locate an element add to cart – demouserabcd Jul 05 '17 at 10:05
  • please check the html code for unique attribute or property name and construct the xpath based on it. for example here, i found that, the add_to_cart is unique across the html dom and used it. please have some knowledge on xpath construction. – Murthi Jul 05 '17 at 12:33
0

First of all, please try not to use absolute xpath locators. This is a very bad strategy of locating elements. Try to have at least relative locators or not have xpaths at all. Please see this post, and this post about using a good Selenium selector.

Secondly, you can use the following selectors, for your given case

driver.findElement(By.cssSelector("div.product_add.ng-scope")).click()

does it successfully.

or even this,

driver.findElement(By.cssSelector("div.button_success.major")).click()

does the same job.

demouser123
  • 4,108
  • 9
  • 50
  • 82