簡體   English   中英

你能告訴我如何在 selenium 中執行最后三行嗎?

[英]Can you tell me how can i execute last three lines in selenium?

在這個程序中,我需要編寫一個腳本來自動化電子商務網站。 需要自動化的主要活動就像... 1 獲取 URL “這已成功執行” 2 將項目添加到購物車“這已成功執行” 3 單擊購物車圖標並繼續結帳。 “這未成功執行”

對於 3 點,我的代碼沒有執行。 不知道為什么?。 我認為我的腳本是寫的,但位置是錯誤的。 請幫我找到它。 謝謝

package siteTesting; import java.util.Arrays; import java.util.List; 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 ItemsAddtoCart { public static void main(String[] args) { String[] itemNeeded = {"Brocolli","Cauliflower","Cucumber"}; System.setProperty("webdriver.gecko.driver", "D:\\Software\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); 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<=noOfProduct; 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(); } } 'these following lines are not executing, can you tell me why?' driver.findElement(By.xpath("//a[@class='cart-icon']/img")).click(); driver.findElement(By.xpath("//button[contains(text(),'PROCEED TO CHECKOUT']")).click(); driver.findElement(By.className("promoCode")).sendKeys("rahulshettyacademy"); } }

請使用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");

Output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM