[英]How to click a specific 'Add to cart' button with respect to a product from product list using Selenium and Java
This my testng class:这是我的 testng class:
public class EcommerceTest
{
public static WebDriver driver;
@Test
public void addtoCartTest() throws InterruptedException
{
driver.get("https://rahulshettyacademy.com/seleniumPractise/");
driver.manage().window().maximize();
EkartPage1 oekart = new EkartPage1(driver);
oekart.AddtoCart();
}
@BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@AfterTest
public void afterTest() {
//driver.close();
}
}
/**/This my page object class**
public class EkartPage1
{
WebDriver driver;
WebDriverWait wait;
@FindBy(xpath = "//button[contains(text(),'ADDED')]")
WebElement addedBtn;
public EkartPage1(WebDriver driver)
{
wait = new WebDriverWait(driver, 30);
PageFactory.initElements(driver, this);
this.driver = driver;
}
**//This is my method to click Add to cart button**
public void AddtoCart() throws InterruptedException /
{
String[] additems = {"Cucumber","Beans"};
List<WebElement> list = driver.findElements(By.cssSelector("h4.product-name"));
for(int i=0;i<list.size();i++)
{
String[] productname = list.get(i).getText().split("-");
String frmtdname = productname[0].trim();
List itemsneeded = Arrays.asList(additems);
if(itemsneeded.contains(frmtdname))
{
List<WebElement> list2 =driver.findElements(By.xpath("//button[text() ='ADD TO CART']"));
list2.get(i).click();
System.out.println("One product added");
}
}
}
I am trying to click on 'Add to cart' for the product 'Beans**.But the selenium webdriver clicks on 'Add to cart' button corresponding to 'Brinjal' which is the next immediate product.Kindly help me to resolve this issue.我正在尝试单击产品“Beans**”的“添加到购物车”。但是 selenium 网络驱动程序单击与下一个直接产品“Brinjal”相对应的“添加到购物车”按钮。请帮助我解决此问题.
To click on ADD TO CARD for Beans you need to use WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies :要单击Beans的ADD TO CARD ,您需要对
elementToBeClickable()
使用WebDriverWait ,并且可以使用以下任一Locator Strategies :
Using xpath :使用xpath :
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/"); String item = "Beans"; new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h4[@class='product-name' and starts-with(., '" +item+ "')]//following::div[2]/button[text()='ADD TO CART']"))).click();
Browser Snapshot:浏览器快照:
The code below worked for me.下面的代码对我有用。 The xpath ("//button[text() ='ADD TO CART']") you have used will not work as when you clicked on first item("Cucumber") the button text changed (from 'ADD TO CART' to '✔ ADDED').
您使用的 xpath ("//button[text() ='ADD TO CART']") 将不起作用,因为当您单击第一项(“Cucumber”)时,按钮文本已更改(从“添加到购物车”到'✔ 添加')。 Within for loop in very short time span the changed button text remain same and in next for loop the added item "Cucumber" excluded.
在很短的时间内在 for 循环中更改的按钮文本保持不变,并且在下一个 for 循环中,添加的项目“Cucumber”被排除在外。 So here it will consider the item next to expected item ("Brinjal" consider as 6th item instead of "Beans")
所以这里它会考虑预期项目旁边的项目(“茄子”被认为是第 6 个项目而不是“豆子”)
So you can use here xpath "//div[@class='product-action']/button" instead of "//button[text() ='ADD TO CART']".所以你可以在这里使用 xpath "//div[@class='product-action']/button" 而不是 "//button[text() ='ADD TO CART']"。
WebDriver Driver = new ChromeDriver();
Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Driver.manage().window().maximize();
String url = "https://rahulshettyacademy.com/seleniumPractise/";
Driver.get(url);
String[] additems= {"Cucumber","Beans"};
AddtoCart(Driver, additems);
}
public static void AddtoCart(WebDriver Driver, String[] additems)
{
List<WebElement> products=Driver.findElements(By.cssSelector("h4.product-name"));
for(int i=0;i<products.size();i++)
{
String[] productname=products.get(i).getText().split("-");
String frmtdname=productname[0].trim();
//format it to get actual vegetable name
//convert array into array list for easy search
// check whether name you extracted is present in arrayList or not-
List itemsneeded = Arrays.asList(additems);
if(itemsneeded.contains(frmtdname))
{
//click on Add to cart
Driver.findElements(By.xpath("//div[@class='product-action']/button")).get(i).click();
}
}
}
You need to add "list = driver.findElements(By.cssSelector("h4.product-name"));"您需要添加“list = driver.findElements(By.cssSelector("h4.product-name"));” this line again in for loop and also change the xpath of add to cart button
这一行再次在 for 循环中,并更改添加到购物车按钮的 xpath
public void AddtoCart() throws InterruptedException /
{
String[] additems = {"Cucumber","Beans"};
List<WebElement> list =
driver.findElements(By.cssSelector("h4.product-name"));
for(int i=0;i<list.size();i++)
{
//added this line again
list = driver.findElements(By.cssSelector("h4.product-name"));
String[] productname = list.get(i).getText().split("-");
String frmtdname = productname[0].trim();
List itemsneeded = Arrays.asList(additems);
if(itemsneeded.contains(frmtdname))
{ //change the xpath of add to cart, take the class name
List<WebElement> list2=driver.findElements(By.xpath("//div[@class='actions']/button"));
list2.get(i).click();
System.out.println("One product added");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.