使用 JavascriptExecutor 的 Selenium Datepicker

IT技术 javascript java selenium selenium-webdriver datepicker
2021-01-29 03:59:50

请告知是否接受这种方法来使用 Selenium 取货

WebDriver driver = new ChromeDriver();

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

driver.get("https://www.spicejet.com/");

Thread.sleep(3000);

JavascriptExecutor js = ((JavascriptExecutor)driver);

js.executeScript ("document.getElementById('ctl00_mainContent_view_date1').removeAttribute('readonly',0);"); 

WebElement onwards_date = driver.findElement(By.id("ctl00_mainContent_view_date1"));
        onwards_date.clear();
        onwards_date.sendKeys("28/02"); 

js.executeScript ("document.getElementById('ctl00_mainContent_view_date2').removeAttribute('readonly',0);"); 

WebElement return_Date = driver.findElement(By.id("ctl00_mainContent_view_date2"));
        return_Date.clear();
        return_Date.sendKeys("27/03"); 
2个回答

使用来自JavascriptExecutor 的SeleniumexecuteScript()方法在网站https://www.spicejet.com/DEPART DATE字段中获取日期您可以使用以下定位器策略

  • 代码块:

      System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
      ChromeOptions options = new ChromeOptions();
      options.addArguments("start-maximized");
      options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
      options.setExperimentalOption("useAutomationExtension", false);
      WebDriver driver =  new ChromeDriver(options);
      driver.get("https://www.spicejet.com/");
      WebElement element = driver.findElement(By.cssSelector("input[name$= 'txt_Fromdate']"));
      ((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('readonly')", element);
      WebElement newElement = driver.findElement(By.cssSelector("input[name$= 'txt_Fromdate']"));
      ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value','28/02')", newElement);
    
  • 浏览器快照:

spicejet出发日期


参考

您可以在以下位置找到一些相关的详细讨论:

您可以使用 JavaScript 设置值以输入ctl00_mainContent_txt_Fromdateid for from datectl00_mainContent_txt_Todateid for to date您不会看到 UI 中的值发生变化,但它确实有效。

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

不是sleep在您的代码中使用WebDriverWait而是使用使 WebDriver 等待特定条件并且仅在需要时等待的时间。

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