简体   繁体   中英

How to Click at all product links, search an element and navigate back.

I am trying to click "boots" related product links one by one, After clicking a product link then perform if-else conditions and then navigate back,doing on this website . I am trying to fetch links like -By.tagName("a"). But I am not able to get links(I am getting output-chrome on XP (74b9af1ba95c1e355e08a2172b279888)] -> tag name: a]).This was the only way available on internet to get links. But I am not able to get links. Here is my code:

 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));

                        }

                    }
                }

There are some issues with your code. Your code has the line mentioned below:

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

Instead of that, it should be:

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

This will return the list containing all the product names. You shouldn't use generic code:

List alllinks = util.getdriver().findElements(By.tagName("a"));

because there are some links in the HTML source which are not interactable on the web page and also you want to click on the products for adding them to your cart.

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