我试图在某些步骤上停止我的硒执行,但我得到了不正确的结果

问题描述 投票:0回答:1

enter image description here我试图在八月日历上停止我的硒执行(While 循环) 但到了九月就停止了 我还想打印 15 日前的日期,但它在 for 循环中打印日历中存在的所有日期,我做错了什么

package class1;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class calendar {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\A0885643\\Desktop\\Selenium-Java\\Day1\\DriverWeb1\\drivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.path2usa.com/travel-companion/");
        driver.manage().window().maximize();
        
        Actions ac1 = new Actions(driver);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        
        
        WebElement language = driver.findElement(By.xpath("//select[@name='form_fields[travel_comp_airline]']"));
        
        js.executeScript("arguments[0].scrollIntoView(true);", language);
        
        Thread.sleep(5000);
        
        WebElement calendar =  driver.findElement(By.xpath("//input[@id='form-field-travel_comp_date']"));
        
        ac1.click(calendar).build().perform();
        
        String months = driver.findElement(By.xpath("//span[@class='cur-month']")).getText();
        
        WebElement nextButton = driver.findElement(By.xpath("//span[@class='flatpickr-next-month']"));
        
        while(!months.equals("August")) {                   
            nextButton.click();
            
            months = driver.findElement(By.xpath("//span[@class='cur-month']")).getText();
            System.out.println("Current month: " + months);
            
        }
        Thread.sleep(5000);
        List<WebElement> dates = driver.findElements(By.xpath("//div[@class='dayContainer']"));
        
        for(int i = 0;i<dates.size();i++) {
            
            System.out.println(dates.get(i).getText());  // not printing till 15    
            if(dates.get(i).getText().equals("15")) { 
                break;              
}
}
}
}
java for-loop selenium-webdriver while-loop
1个回答
0
投票

关于月份:当您点击箭头转到下个月时,有一个过渡动画。当您从 7 月过渡到 8 月时,代码会在更改为 8 月之前读取 7 月的值,然后单击下一步按钮。因此它在九月停止。在进入 if 条件的下一个周期之前,您需要稍等一下,要么显式等待一小段时间,要么等待转换完成。

关于日期:

days
元素的xpath实际上指向日历中所有日期的容器。因此,您最终会得到一个元素的列表并打印该元素的文本。您需要更改 xpath 以指向日期元素。

© www.soinside.com 2019 - 2024. All rights reserved.