简体   繁体   English

Java Selenium WebDriver无法将第一个项目放入购物篮

[英]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 以下是简短程序,并在以下网站中: 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 使用Java 1.8 Selenium WebDrive版本#2.48 Mac OS版本#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. 您可以在每次点击事件之前放置wait.until()方法。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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