2016-10-13 2 views
1

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가 org.openqa.selenium.WebElement

캐스트 할 수없는
+0

감사 – jackyghai

+0

당신은 나에게 내가 캘린더 요소를 찾기 위해 사용해야 올바른 실제 XPath는 제안시겠습니까 .. : 여기에 코드를 해결하기 위해 몇 가지 예입니다? 나는 xpath를 사용하는 것을 좋아하지 않기 때문에 같은 것에 대한 제안이 필요하다. 나는 현재 날짜 나 현재 날짜 옆의 날짜를 찾아야한다. 실제 Xcode는 무엇이되어야합니까? (vipcars.com) – jackyghai

+0

현재 날짜를 찾으려면'td [@ class = 'ui-datepicker-days-cell-over ui-datepicker-today']'를 사용하십시오. 당신의 xpath에서 10 월 14 일과 11 월 18 일을 얻습니다. – Maze

답변

0

코드의 문제는 (List<WebElement>) ((WebElement) driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr[3]/td[6]")); List to WebElement를 캐스팅하는 것이 불가능합니다. 그 후에는 List에 캐스팅하려고합니다. 이것은 또한 가능하지 않습니다.

무엇을 원하며 무엇을하고 싶습니까? 회신 lrnzcig에 대한

//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(); 
} 
+0

안녕하세요 @ lrnzcig 사용중인 xpath를 확인해 주시겠습니까? – jackyghai

관련 문제