unable to select ‘Add to cart’ button

Hi

Same question being asked few times but when i trying to use any of the below xpath in order to click on “add to cart” in  “demoqa.com”  then

 

driver.findElement(By.xpath(“(.//*[@class=’input-button-buy’])[1]”)).click();

driver.findElement(By.xpath(“.//input[@value=’Add to Cart’][1]”)).click();
driver.findElement(By.xpath(.//*[@class=’wpsc_buy_button_container group’]/div[1]/span/input[@name=’Buy’])[1].click

driver.findElement(By.xpath(.//*[@id=’default_products_page_container’]/div[3]/div[2]/form/div[2]/div[1]/span/input).click

 

****************************************

package dropdown;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Dropdown_test {

public static void main(String[] args) {
// TODO Auto-generated method stub

try{
System.setProperty(“webdriver.gecko.driver”, “P:\\plugin\\gecko\\geckodriver.exe”);
WebDriver driver=new FirefoxDriver();
driver.get(“http://www.store.demoqa.com”);
driver.findElement(By.xpath(“.//*[@id=’account’]/a”)).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath(“.//*[@id=’log’]”)).sendKeys(“Test_user_21”);
driver.findElement(By.xpath(“.//*[@id=’pwd’]”)).sendKeys(“Test#123”);
driver.findElement(By.xpath(“.//*[@id=’login’]”)).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.findElement(By.xpath(“.//*[@id=’menu-item-33′]”)).click();

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath(“(.//*[@class=’input-button-buy’])[1]”)).click();
//driver.findElement(By.xpath(“.//input[@value=’Add to Cart’][1]”)).click();

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

// driver.quit();
}
catch(Exception e)

{
e.printStackTrace();
}

}}

 

 

Then the button is not getting clicked and neither an error being displayed.

If some one can please guide where I am making the mistake.

 

Thanks

 

 

Default Asked on March 22, 2017 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)

    This will work: driver.findElement(By.xpath(“//*[@name=’Buy’]”)).click();

    Hope it helps you out 🙂

    Supporter Answered on April 6, 2017.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.