Please advise if this approach is accepted to pick-up date using Selenium
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("/");
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");
Please advise if this approach is accepted to pick-up date using Selenium
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.spicejet./");
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");
Share
Improve this question
edited Feb 26, 2020 at 16:26
undetected Selenium
194k44 gold badges303 silver badges381 bronze badges
asked Feb 26, 2020 at 3:51
BimleshBimlesh
2812 gold badges9 silver badges21 bronze badges
2 Answers
Reset to default 2To pick-up a date within the DEPART DATE field in the website https://www.spicejet./ using Selenium's executeScript() method from JavascriptExecutor you can use the following Locator Strategies:
Code Block:
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./"); 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);
Browser Snapshot:
References
You can find a couple of relevant detailed discussions in:
- Is there a way to add a new attribute with a value to an element using selenium python?
- How to use javascript to set attribute of selected web element using selenium Webdriver using java?
- Python - How to edit href attribute in source code before clicking using Selenium
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./");
// 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();
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745182766a4615495.html
评论列表(0条)