I tried adding a product in cart by clicking "Add to Cart" in flipkart page.
Here is the following HTML code:
<input type="submit" class="btn-buy-now btn-big current xh-highlight" data-buy-listing-id="LSTWATE8VGKTHD9HMJBFJKNBI" data-listing-metrics="TO_BE_IMPLEMENTED" data-seller-id="r7wrdtz4dsn1nm0k" data-pid="WATE8VGKTHD9HMJB" data-vertical="watch" data-disable-cart="true" data-is-large="false" data-is-in-cart="false" data-is-pin-serviceable="true" data-is-pin-available="false" data-is-listing-buyable="true" value="Add to Cart">
I customized my own xpath in 2 different ways:
By selecting the value attribute: //*[@value='Add to Cart']
By traversing through the tagnames: .//*[@class='product new-branding']/div/div[4]/div/div[7]/div/div[3]/div/div/div[5]/div/div[2]/div/div/div[2]/div/div/form/input[8]
.
But nothing worked. I got an error message as
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='btn-buy-now btn-big current xh-highlight']"}
Note: I have done in 2 ways as mentioned above. I commented out the line where I created xpath by traversing through the tagnames
Here is the following code:
public class flipkart {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\NIVEDA_B\\Desktop\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.flipkart.com/britex-bt3106-octane-ultimate-chronograph-pattern-analog-watch-boys-men/p/itme8vgk7fpzxrrq?pid=WATE8VGKTHD9HMJB&al=ij0R90nZ4Ylmh5ZtBuTS38ldugMWZuE7Phn6Yd2VMSJ4UT8n2g9QLXDX%2Bt6tGNzYWbjBKAf9tCc%3D&offer=nb%3Amp%3A0511b3c322&ref=L%3A-5467549183610437135&srno=b_4&findingMethod=Deals%20of%20the%20Day&otracker=hp_omu_Deals%20of%20the%20Day_3_3649a958-7a27-4c8b-b7d2-f6f05ab49a8e_0");
driver.findElement(By.xpath("//*[@class='btn-buy-now btn-big current xh-highlight']")).click();
//driver.findElement(By.xpath("//*[@class='product new-branding']/div/div[4]/div/div[7]/div/div[3]/div/div/div[5]/div/div[2]/div/div/div[2]/div/div/form/input[8]/")).click();
driver.findElement(By.xpath("//*[@value='Add to Cart']")).click();
}
}
I just tried this and it worked. You will need to pause between clicking Add to Cart and then clicking the CART button.
driver.findElement(By.partialLinkText("CART")).click();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.