繁体   English   中英

对于大小为3的WebElement列表的循环,在第一个实例之后单击同一链接

[英]For loop with a size 3 WebElement List clicking on the same link after first instance

我在我们网站的便当盒组中有3个链接。 如果链接文本符合我的条件,我需要单击特定的链接(无论如何我都需要单击所有3个链接,并且在单击它们之后需要执行不同的操作)。 我在for循环中编写了if循环来实现这一点。

当我执行此操作时,对于第一个循环(即i = 0),它单击正确的链接。 对于第二个循环(i = 1),我希望它选择第二个链接。 但是它单击了第三个链接。 对于第三个循环,也单击第三个链接。 不确定我在这里缺少什么。

public void SeasonalLinks() throws InterruptedException
{
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    List <WebElement> Seasonallnks = driver.findElements(By.xpath("//section[@id='seasonallinks']/ul/li/div/a"));
    int sl = Seasonallnks.size();
    System.out.println("Total no.of seasonal Links: "+sl);
    int i=0;

    for (i=0;i<sl;i++)
    {
        List <WebElement> Seasonallnks1 = driver.findElements(By.xpath("//section[@id='seasonal-links']/ul/li/div/a"));
        String sl1 = Seasonallnks1.get(i).getText();

        if (sl1.equalsIgnoreCase("Start a provider search"))
        {
            Seasonallnks1.get(i).click();
            WebDriverWait pshome = new WebDriverWait(driver,20);
            pshome.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")));
            Thread.sleep(5000);
            driver.findElement(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")).click();
        }
        else if (sl1.equalsIgnoreCase("Use pharmacy self-service tools"))
        {
            Seasonallnks1.get(i).click();
            WebDriverWait phome = new WebDriverWait(driver,20);
            phome.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")));
            Thread.sleep(5000);
            WebDriverWait iswait1 = new WebDriverWait(driver,30);
            iswait1.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='acc-spinner-container]")));
            driver.findElement(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")).click();
        }

        else if (sl1.equalsIgnoreCase("Start rewarding yourself today"))
        {
            Seasonallnks1.get(i).click();
            WebDriverWait hhome = new WebDriverWait(driver,20);
            hhome.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")));
            driver.findElement(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")).click();
        }
    }

一些反馈...为了使您的代码更具可维护性并节省精力,您应遵循DRY原则 (消除重复性代码)。 如果您看一下if-else链,那里会有很多重复。 每次单击相同的链接时,请等待元素可单击并单击它。 对于单个元素,实际上只有一件独特的事情要做。

应避免使用Thread.sleep() (谷歌查看完整说明)。 您已经在使用WebDriverWait ,这是一个好习惯,因此,如果需要,可以添加另一个WebDriverWait以避免Thread.sleep()使用。

您有两个集合, SeasonallnksSeasonallnks1 ,它们都存储相同的元素,但是第二个实例看起来像在定位器中有错字。 我将两者结合在一起。

在驱动程序的整个生命周期中, implicitlyWait()超时仅需设置一次,并且可能应在此函数之外设置。

int i = 0;的声明int i = 0; 可以/应该在for循环声明中完成。

您声明了两次等待,但是您可以重用第一次等待,以使代码更简洁。

如果多次使用定位器,请考虑将其声明为By类型的变量,以方便重用。 它避免了拼写错误,并且在需要时更易于更改,因为它只需要在一个地方进行更改。

wait.until()返回等待的元素,因此当您等待元素可单击然后打算单击它时,您可以在末尾添加.click() ,例如wait.until(...).click();

您还应该努力拥有更多描述性的变量名。 它会在几个月后或其他需要阅读您的代码并弄清楚它在做什么的人中为您提供帮助。

希望您能从下面的清理代码中看到通过使用DRY并应用上面列出的其他一些建议可以节省多少行。

public void SeasonalLinks() throws InterruptedException
{
    By seasonalLinksLocator = By.xpath("//section[@id='seasonallinks']/ul/li/div/a");
    List<WebElement> seasonalLinks = driver.findElements(seasonalLinksLocator);
    System.out.println("Total no.of seasonal Links: " + seasonalLinks.size());
    WebDriverWait wait = new WebDriverWait(driver, 20);
    for (int i = 0; i < seasonalLinks.size(); i++)
    {
        String seasonalLinkText = seasonalLinks.get(i).getText();
        seasonalLinks.get(i).click();
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']"))).click();
        if (seasonalLinkText.equalsIgnoreCase("Use pharmacy self-service tools"))
        {
            wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='acc-spinner-container]")));
        }
        seasonalLinks = driver.findElements(seasonalLinksLocator);
    }
}

暂无
暂无

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

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