如何单击所有产品链接,搜索元素并导航回来。

时间:2018-06-07 07:12:24

标签: java selenium automation tagname

我试图逐个点击“靴子”相关产品链接,点击产品链接然后执行if-else条件,然后导航回来,对此website进行操作。我试图获取像-By.tagName(“a”)这样的链接。但是我无法获得链接(我在XP上获得了输出chrome(74b9af1ba95c1e355e08a2172b279888)] - >标签名称:a])。这是互联网上获取链接的唯一方式。但我无法获得链接。这是我的代码:

 public class GuestShoppingTestCase {

        UtilityMethods util = new UtilityMethods();

        @BeforeSuite
        public void launchBrowser() {

            UtilityMethods.openBrowser(Constants.BROWSER_NAME);
            UtilityMethods.launchWebsite(Constants.URL);

        }

        @Test

        public void PurchaseItemTest() throws InterruptedException, IOException {


            Thread.sleep(5000);
            try {

                util.getdriver().switchTo().alert().dismiss();

            } catch (Exception e) {
                final By DROPDOWN = By.cssSelector("li[class='atg_store_dropDownParent']");
                final By DROPDOWN_LINK = By.cssSelector("a[class='accord-header ']");

                // Navigate to the Women Category through Dropdowns

                List<WebElement> dropdowns = new WebDriverWait(util.getdriver(), 15)
                        .until(ExpectedConditions.presenceOfAllElementsLocatedBy(DROPDOWN));

                WebElement women = (WebElement) dropdowns.stream()
                        .flatMap(dropdown -> dropdown.findElements(DROPDOWN_LINK).stream())
                        .filter(link -> link.getText().equals("WOMEN")).findFirst().orElse(null);

                if (women != null) {
                    new WebDriverWait(util.getdriver(), 15).until(ExpectedConditions.elementToBeClickable(women));
                    Actions action = new Actions(util.getdriver());
                    action.moveToElement(women).build().perform();

                    // Search and Click a sub-category "Boots"

                    new WebDriverWait(util.getdriver(), 20)
                            .until(ExpectedConditions.elementToBeClickable(util.getdriver().findElement(By.xpath("//a[@title='Boots']"))))
                            .click();
                    // Finding all links and saving in a list------------

                Thread.sleep(10000);

                util.getdriver().findElement(By.id("atg_store_prodList"));
                    List<WebElement> alllinks = util.getdriver().findElements(By.tagName("a"));

// Printing all links-------
                    System.out.println(alllinks);

                    for (int i = 6; i < alllinks.size(); i++) {
                        System.out.println(alllinks.get(i));
                        WebElement elementToBeClicked = alllinks.get(i);
                        elementToBeClicked.click();
                        util.getdriver().findElement(By.id("atg_behavior_addItemToCart")).click();
                        // util.getdriver().switchTo().alert().dismiss();
                        if (util.getdriver().findElement(By.xpath("//a[contains(text(),'Continue Shopping')]"))
                                .isDisplayed()) {
                            util.getdriver().findElement(By.xpath("//a[contains(text(),'Continue Shopping')]")).click();
                            util.getdriver().navigate().back();

                        }

                        else {

                            util.getdriver().findElement(By.xpath("//a[@title='Checkout']")).click();
                            Select selectCountry = new Select(
                                    util.getdriver().findElement(By.id("atg_store_countryNameSelect")));
                            selectCountry.selectByValue("US");
                            Thread.sleep(3000);
                            util.clickbyXpath(Constants.PROCEEDTOCHECKOUT);
                            util.getdriver().findElement(By.id("atg_store_catSubProdList"))
                                    .sendKeys(BarneyTestData.getvalueofexcel(4, 1));

                        }

                    }
                }

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。您的代码具有以下提及的行:

util.getdriver().findElement(By.id("atg_store_prodList"));
List<WebElement> alllinks = util.getdriver().findElements(By.tagName("a"));

而不是那样,它应该是:

WebElement prodList = util.getdriver().findElement(By.id("atg_store_prodList"));
List<WebElement> alllinks = prodList.findElements(By.xpath(".//div[@class='product-name']/a"));

这将返回包含所有产品名称的列表。你不应该使用通用代码:

列出alllinks = util.getdriver()。findElements(By.tagName(&#34; a&#34;));

因为HTML源代码中有一些链接无法在网页上进行交互,您还想点击产品以将其添加到购物车中。