gpt4 book ai didi

java - 通过 selenium webdriver 选择日期

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:21 24 4
gpt4 key购买 nike

我无法使用 xpath 选择日期或找到实际日期,或者我想在 vipcars.com 上选择当前日期旁边的一个日期。这是我正在使用的代码..

public static void main(String args[])
{

System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");//any driver can be initialized like this just by replacing driver here.
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setCapability("chrome", true);
WebDriver driver = new ChromeDriver(capabilities);

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

// Test URL
driver.get("https://www.vipcars.com/");

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String parent = driver.getWindowHandle();
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[id='supplycarsIframe']")));

driver.findElement(By.id("pickup_country")).click();
Select country = new Select(driver.findElement(By.id("pickup_country")));
country.selectByValue("441");


driver.findElement(By.id("pickup_city")).click();
Select city = new Select(driver.findElement(By.id("pickup_city")));
city.selectByValue("9747");

driver.findElement(By.id("pickup_location")).click();
Select location = new Select(driver.findElement(By.id("pickup_location")));
location.selectByValue("14927");

driver.findElement(By.id("pickdate")).click(); //calender clicks
driver.findElement(By.xpath("//input[@placeholder='From']")); //id found
**driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr[3]/td[6]"));**



List<WebElement> allDates = (List<WebElement>) ((WebElement) driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr[3]/td[6]"));
(WebElement) allDates).click();

控制台错误消息:

java.util.ArrayList cannot be cast to org.openqa.selenium.WebElement

最佳答案

您的代码中的问题是 (List<WebElement>) ((WebElement) driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr[3]/td[6]"));您将 List 转换为 WebElement,这是不可能的。之后你想将其转换为 List,这也是不可能的。

你期望什么,你想做什么?以下是一些纠正代码的示例:

//this will return a List<WebElement> 
driver.findElements(Locator locator);

//this will return a WebElement
driver.findElement(Locator locator);

//if you know that there is only one element which you want to click, use this
driver.findElement(By.xpath("//table[@class='ui-datepicker-calendar']//tr[3]/td[6]")).click();

//if there are more than one WebElements which can be found by that xpath locator, use this
List<WebElement> allDates = driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr[3]/td[6]"));
//now you can iterate through the list and do what you want, e.g. click every element
for (WebElement date : allDates) {
date.click();
}

关于java - 通过 selenium webdriver 选择日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40015062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com