Hi Sir,
I have a scenario where i need to click on add button of particular item. What should be the approach in this.
I am able to extract all texts available on the page but i want to click on add button of item say "Icecream"
This is for swiggy appl
kindly see the screenshot
All add buttons have same classes. How would selenium get to know which add button should i need to click on based on selected text. pls help
Instructor
Ashish Thakur Replied on 12/04/2019
See, it is all about generating a good custom XPath
Try generating a XPath like this below
//*[@id="menu-content"]/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div[3]/div[2]/div[1]
All you need to do is tweaking few things
driver.get("https://www.swiggy.com/bangalore/truffles-ice-spice-central-bangalore");
Thread.sleep(2000);
List<WebElement> add=driver.findElements(By.xpath("//div[text()='ADD']"));
List<WebElement> prod=driver.findElements(By.xpath("//div[@itemprop='name']"));
//*[@id="h-1950595611"]/div[2]/div[1]/div/div[3]/div[2]/div[1]
//*[@id="h-1950595611"]/div[2]/div[2]/div/div[3]/div[2]/div[1]
String prodName="Chocolate Caramel";
System.out.println(prod.size());
for(int i=0;i<prod.size();i++) {
if(prodName.equals(prod.get(i).getText()))
{
for(int j=0;j<add.size();j++) {
//System.out.println("Name matched");
add.get(j).click();
}
}
}
Instructor
Ashish Thakur Replied on 12/04/2019
Try avinash's code as well. But make sure that XPath is effective and doesn't depend on dynamic values
Thanks sir... I will try avinash code and get back to you.