Java 如何单击所有产品链接、搜索元素并返回。

Java 如何单击所有产品链接、搜索元素并返回。,java,selenium,automation,tagname,Java,Selenium,Automation,Tagname,我试图一个接一个地点击“boots”相关的产品链接,点击一个产品链接后,执行if-else条件,然后导航回来,这样做。我正在尝试获取类似于-By.tagName(“a”)的链接。但我无法获得链接(我在XP上获得输出chrome(74b9af1ba95c1e355e08a2172b279888)->标记名:a)。这是在internet上获得链接的唯一方法。但我无法获得链接。这是我的密码: public class GuestShoppingTestCase { UtilityM

我试图一个接一个地点击“boots”相关的产品链接,点击一个产品链接后,执行if-else条件,然后导航回来,这样做。我正在尝试获取类似于-By.tagName(“a”)的链接。但我无法获得链接(我在XP上获得输出chrome(74b9af1ba95c1e355e08a2172b279888)->标记名:a)。这是在internet上获得链接的唯一方法。但我无法获得链接。这是我的密码:

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

                        }

                    }
                }
公共类GuestShoppingTestCase{
UtilityMethods util=新的UtilityMethods();
@套房前
公共void启动浏览器(){
实用方法.openBrowser(常量.BROWSER\u名称);
启动网站(Constants.URL);
}
@试验
public void PurchaseItemTest()引发InterruptedException,IOException{
睡眠(5000);
试一试{
util.getdriver().switchTo().alert().disease();
}捕获(例外e){
最终By DROPDOWN=By.cssSelector(“li[class='atg_store_dropdownport']”);
最终按下拉菜单链接=按.cssSelector(“a[class='accord-header']);
//通过下拉菜单导航到“女性”类别
列表下拉列表=新建WebDriverWait(util.getdriver(),15)
。直到(预期条件。通过(下拉菜单)显示的所有事件);
WebElement women=(WebElement)下拉列表。stream()
.flatMap(下拉菜单->下拉菜单.findElements(下拉菜单链接).stream())
.filter(link->link.getText().equals(“女性”)).findFirst().orElse(null);
if(女性!=null){
新的WebDriverWait(util.getdriver(),15).until(ExpectedConditions.elementtobelickable(women));
Actions action=新操作(util.getdriver());
action.moveToElement(women.build().perform();
//搜索并单击子类别“Boots”
新的WebDriverWait(util.getdriver(),20)
.until(ExpectedConditions.ElementToBickable(util.getdriver().findElement(By.xpath(//a[@title='Boots']))
。单击();
//查找所有链接并保存在列表中------------
睡眠(10000);
util.getdriver().findElement(By.id(“atg_store_prodList”);
列出所有链接=util.getdriver().findElements(按.tagName(“a”));
//打印所有链接-------
System.out.println(所有链接);
对于(int i=6;i
您的代码有一些问题。您的代码有下面提到的行:

util.getdriver().findElement(By.id("atg_store_prodList"));
List<WebElement> alllinks = util.getdriver().findElements(By.tagName("a"));
util.getdriver().findElement(By.id(“atg_store_prodList”);
列出所有链接=util.getdriver().findElements(按.tagName(“a”));
相反,它应该是:

WebElement prodList = util.getdriver().findElement(By.id("atg_store_prodList"));
List<WebElement> alllinks = prodList.findElements(By.xpath(".//div[@class='product-name']/a"));
WebElement prodList=util.getdriver().findElement(By.id(“atg_store_prodList”);
List alllinks=prodList.findElements(By.xpath(“.//div[@class='product-name']/a”);
这将返回包含所有产品名称的列表。您不应使用通用代码:

列出所有链接=util.getdriver().findElements(按.tagName(“a”);


因为HTML源中有一些链接在网页上不可交互,而且您希望单击产品以将其添加到购物车中。

您到底停留在哪一行?我停留在------WebElement ElementToBelicked=alllinks.get(I);ElementToBelicked.click();……因为可能是元素没有在第行提取----List alllinks=util.getdriver().findElements(By.tagName(“a”);但是我无法单击第二个产品链接,如何解决这个问题?