Selenium Datepicker using JavascriptExecutor

前端 未结 2 485
生来不讨喜
生来不讨喜 2020-12-04 03:33

Please advise if this approach is accepted to pick-up date using Selenium

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

drive         


        
2条回答
  •  死守一世寂寞
    2020-12-04 04:31

    You can set value using JavaScript to input with ctl00_mainContent_txt_Fromdate id for from date and ctl00_mainContent_txt_Todate id for to date. You'll not see value changing from UI, but it works.

    js.executeScript("arguments[0].value = arguments[1]",
        driver.findElement(By.id("ctl00_mainContent_txt_Fromdate")), "28-02-2020");
    

    Instead of using sleep in you code, use WebDriverWait that makes WebDriver wait for a certain condition and will wait only as long as required.

    WebDriver driver = new ChromeDriver();
    WebDriverWait wait = new WebDriverWait(driver, 10);
    JavascriptExecutor js = (JavascriptExecutor) driver;
    driver.manage().window().maximize();
    
    driver.get("https://www.spicejet.com/");
    
    // Wait for Search button to be clickable, the state in which we assume that the site has loaded
    WebElement searchButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_btn_FindFlights")));
    
    // Select From and To Cities
    
    js.executeScript("arguments[0].value = arguments[1]",
            driver.findElement(By.id("ctl00_mainContent_txt_Fromdate")), "28-02-2020");
    
    js.executeScript("arguments[0].value = arguments[1]",
            driver.findElement(By.id("ctl00_mainContent_txt_Todate")), "01-03-2020");
    
    searchButton.click();
    

提交回复
热议问题