Пожалуйста, проверьте ниже решение с WebDriverWait
:
WebDriverWait wait = new WebDriverWait (driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='cart-icon']//img[contains(@class,'')]"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'PROCEED TO CHECKOUT')]"))).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@placeholder='Enter promo code']"))).sendKeys("PramoCode");
Примечание: пожалуйста, добавьте ниже импорта:
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.WebDriverWait;
Рабочий раствор
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> products = driver.findElements(By.className("product-name"));
int noOfProduct = products.size();
System.out.println(noOfProduct);
int i;
for(i=0; i<=itemNeeded.length; i++)
{
String[] NameofProduct = products.get(i).getText().split("-");
String uNameofProduct = NameofProduct[0].trim();
List itemNeededList = Arrays.asList(itemNeeded);
if(itemNeededList .contains(uNameofProduct))
{
System.out.println(uNameofProduct);
driver.findElements(By.xpath("//div[@class='product-action']")).get(i).click();
}
}
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='cart-icon']//img[contains(@class,'')]"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'PROCEED TO CHECKOUT')]"))).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@placeholder='Enter promo code']"))).sendKeys("PramoCode");
Выход: