Issue
enter image description hereI am trying to stop my selenium execution on August month calendar(While loop) but it stops on the month of September and also I want to print dates till 15 but it is printing all the dates present in the calendar in for loop what wrong am I doing
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;
}
}
}
}
Solution
Regarding months: when you click the arrow to go to next month, there is a transition animation present. When you transition from July to August, the code reads the value of July before it is changed to August and then clicks the next button. Thus it stops on September. You would need to wait a bit, either explicitly wait for a short time or for the transition to finish, before going to the next cycle for your if condition.
Regarding days: the xpath of your days
element actually points to the container of all the days in the calendar. Therefore you end up with a list of one element and print the text of that element. You need to change the xpath to point to a day element. You could use this xpath for day element: //*[@class='flatpickr-day ']
. (Notice the space at the end of the class name).
Also consider changing variable names: months -> month
and dates -> days
to better represent the values stored in those variables.
Answered By - Shatas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.