admin管理员组文章数量:1125725
Please help me understand how to pick the date from the date picker. I tried options 1 and 2, below, but I get NoSuchElementException
.
driver.get("/");
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
driver.findElement(By.xpath("//a[@value='BLR']")).click();
driver.findElement(By.xpath("(//a[@value='MAA'])[2]")).click();
Thread.sleep(5000);
// 1
driver.findElement(By.xpath("(//a[@class='ui-state-default ui-state-active ui-state-hover'])[1]")).click();
// 2
driver.findElement(By.cssSelector("a[class='ui-state-default ui-state-highlight']")).click();
Please help me understand how to pick the date from the date picker. I tried options 1 and 2, below, but I get NoSuchElementException
.
driver.get("https://rahulshettyacademy.com/dropdownsPractise/");
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
driver.findElement(By.xpath("//a[@value='BLR']")).click();
driver.findElement(By.xpath("(//a[@value='MAA'])[2]")).click();
Thread.sleep(5000);
// 1
driver.findElement(By.xpath("(//a[@class='ui-state-default ui-state-active ui-state-hover'])[1]")).click();
// 2
driver.findElement(By.cssSelector("a[class='ui-state-default ui-state-highlight']")).click();
Share
Improve this question
edited Jan 9 at 5:23
JeffC
25.5k5 gold badges34 silver badges55 bronze badges
asked Jan 9 at 5:13
amalamal
3,57010 gold badges30 silver badges45 bronze badges
1
|
1 Answer
Reset to default 2Your locators were not correct. Also, you need to add WebDriverWait
s for all of these elements because the airport and date panels don't show up instantly.
I rewrote your code to use methods for setting the origination airport, destination airport, and departure date. This makes the code much easier to read and to reuse. Ideally you'd put all of this into a page object but I didn't go that far. I'll leave that as an exercise for you to investigate.
import java.time.Duration;
import java.time.LocalDate;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SeleniumSandbox {
static WebDriver driver;
static WebDriverWait wait;
public static void main(String[] args) throws Exception {
String url = "https://rahulshettyacademy.com/dropdownsPractise";
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
setOriginationAirport("BLR");
setDestinationAirport("MAA");
setDepartureDate(LocalDate.of(2019, 5, 25));
driver.quit();
}
public static void setDepartureDate(LocalDate departureDate) {
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='ui-datepicker-div']//td[@data-month='" + departureDate.getMonthValue() + "']/a[text()='" + departureDate.getDayOfMonth() + "']"))).click();
}
public static void setDestinationAirport(String airportCode) {
wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_ddl_destinationStation1_CTXT"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#ctl00_mainContent_ddl_destinationStation1_CTNR table a[value='" + airportCode + "']"))).click();
}
public static void setOriginationAirport(String airportCode) {
wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_ddl_originStation1_CTXT"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#ctl00_mainContent_ddl_originStation1_CTNR table a[value='" + airportCode + "']"))).click();
}
}
本文标签: javaHow to pick the date from dropdown date picker using SeleniumStack Overflow
版权声明:本文标题:java - How to pick the date from dropdown date picker using Selenium - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736671783a1946961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ui-state-hover
orui-state-highlight
? I went to check the webpage with developer tools. itsui-state-default ui-state-active
orui-state-default
– pebble unit Commented Jan 9 at 5:43