简体   繁体   中英

Java Selenium WebDriver unable to place first item into basket

Following is short program & in the following web site: https://uk.webuy.com/search/index.php?stext= *&section=&catid=956

I am trying to click the first three product's "I want to buy this item" button & view them in the VIEW BASKET at the right side of the page.

For some reason, I am able to see the second and third product only. For some reason, the first product never makes it to the basket, & it does not produce an error.

Only when I change the following line:

 allButtons.get(0).click();

to:

allButtons.get(0).click();
  allButtons.get(0).click();
  allButtons.get(0).click();

I will see one occurrence of the first product in the basket.

What am I doing wrong? Is there something missing that is causing this problem?

Using Java 1.8 Selenium WebDrive Version #2.48 Mac OS Version #10.11.13

Thank you

public class ZWeBuy {


        static WebDriver driver;

        @Test
        public void testProductPurchaseProcess()  {     
            driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.manage().window().maximize();
            driver.get("https://uk.webuy.com/search/index.php?stext=*&section=&catid=956");
            closePopupIfPresent();

            //xpath for all product names in this page
            List<WebElement> allNames = driver.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
            List<WebElement> allButtons = driver.findElements(By.xpath("//div[@class='action']/div/a[2]/div/span"));                                                    

            System.out.println("Total names = "+ allNames.size());
            System.out.println("Total buttons = "+ allButtons.size());
            System.out.println("I= " + 0 + " PRDCT: --- " +allNames.get(0).getText());
            allButtons.get(0).click();
            WebDriverWait wait = new WebDriverWait(driver,120);
            wait.until(ExpectedConditions.elementToBeClickable(By.xpath("html/body/div[5]/div[1]/div[3]/div[5]/div[1]/div[1]/div[3]/div/a[2]/div/span")));
            System.out.println("I= " + 1 + " PRDCT: --- " +allNames.get(1).getText());
            allButtons.get(1).click();  
            System.out.println("I= " + 2 + " PRDCT: --- " +allNames.get(2).getText());
            allButtons.get(2).click();


            }

            public static void closePopupIfPresent(){

                Set<String> winIds = driver.getWindowHandles();
                System.out.println("Total windows -> "+ winIds.size());

                if(winIds.size() == 2){
                    Iterator<String> iter = winIds.iterator();
                    String mainWinID = iter.next();
                    String popupWinID = iter.next();
                    driver.switchTo().window(popupWinID);
                    driver.close();
                    driver.switchTo().window(mainWinID);

                }

            }
}

Your browser could not render that first button. you may put wait.until() method before each click event. try this

    WebDriverWait wait = new WebDriverWait(driver,120);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[5]/div[1]/div[3]/div[5]/div[1]/div[1]/div[3]/div/a[1]/div/span")));
    allButtons.get(0).click();

Your code isn't functional. Popup closing logic doesn't work, its actually not a separate window, its a dialog box within the same window. You should also consider simplifying your selectors. Alright enough said, here is working and tested code.

WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("https://uk.webuy.com/search/index.php?stext=*&section=&catid=956");
WebElement element;
try {
    element = driver.findElement(By.cssSelector(".deliver-component-wrapper>a>div"));
    System.out.println("Closing pop up");
    element.click();
} catch (NoSuchElementException e) {
    System.out.println("Alright, no such dialog box, move on");
}

List<WebElement> buyButtons = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector(
        "span.listBuyButton_mx")));
Assert.assertTrue("Less than three buttons found", buyButtons.size() >= 3);
for (int i = 0; i < 3; i++) {
    WebElement buyButton = buyButtons.get(i);
    wait.until(ExpectedConditions.elementToBeClickable(buyButton)).click();
    System.out.println("Clicked Buy Button " + (i + 1));
}
WebElement basketCount = wait
        .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#buyBasketRow>td.basketTableCell")));
System.out.println(basketCount.getText());
driver.quit(); 

It prints

Closing pop up
Clicked Buy Button 1
Clicked Buy Button 2
Clicked Buy Button 3
3 item/s

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM