2016-09-27 113 views
0

我正在使用Selenium WebDriver進行我的Web自動化測試並使用Java編寫腳本。如何在出生日期的Selenium WebDriver中選擇日期選取器

這是一個開始的美好的一天,直到我不得不停止在「選擇日期的誕生」。我想知道如何在日期選擇器下拉列表中選擇日期,月份和年份的值。我想選擇的日期是19-Dec-1966。

地址:http://radcard-cloud.idatamap.com/login

Photo of the calendar

這是我最後的代碼,我試試。我花了將近4天的時間試圖完成它。請幫助!

package iDMautomation; 
 
    
 
import org.openqa.selenium.By; 
 
import org.openqa.selenium.Keys; 
 
import org.openqa.selenium.WebDriver; 
 
import org.openqa.selenium.WebElement; 
 
import org.openqa.selenium.chrome.ChromeDriver; 
 
import org.openqa.selenium.support.ui.ExpectedConditions; 
 
import org.openqa.selenium.support.ui.Select; 
 
import org.openqa.selenium.support.ui.WebDriverWait; 
 
import java.util.List; 
 
import org.openqa.selenium.firefox.*; 
 
import java.util.concurrent.*; 
 

 
public class FirstTest { 
 
     
 

 
\t public static void main(String[] args) { 
 
      
 
     //initialize Chrome driver 
 
     System.setProperty("webdriver.chrome.driver", "C:\\Users\\senn\\Desktop\\RadCard Test\\Selenium\\chromedriver_win32\\chromedriver.exe"); 
 
     WebDriver driver = new ChromeDriver(); 
 
     
 
      
 
     //Open gmail 
 
     driver.get("http://radcard-cloud.idatamap.com/"); 
 
     driver.manage().window().maximize(); 
 
      
 
     //driver.findElements(By.xpath(xpathExpression)) 
 
     // Enter userd id 
 
      
 
     driver.findElement(By.className("patient")).click(); 
 
      
 
     //email pedram 
 
     //driver.findElement(By.className("providerNum")).click(); 
 
     WebDriverWait wait = new WebDriverWait(driver, 20);// 1 minute 
 
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email"))); 
 

 
      
 
     driver.findElement(By.id("email")).click(); 
 
     driver.findElement(By.id("email")).sendKeys("[email protected]"); 
 
      
 
     driver.findElement(By.id("PIN")).sendKeys("2281"); 
 
      
 
     driver.findElement(By.id("DOB")).click(); 
 
      
 
     
 
     Select sel1=new Select(driver.findElement(By.cssSelector("DOB")));  
 
     WebElement FirstTest = driver.findElement(By.id("DOB")); 
 
      
 
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
 
     driver.findElement(By.className("ui-datepicker-current-day")).click(); 
 
      
 
     Select sel3=new Select(driver.findElement(By.cssSelector("selectDay"))); 
 
      
 
     new Select((WebElement) sel3).selectByVisibleText("14");  
 
      
 
     FirstTest.sendKeys(Keys.ENTER);
所有的

+0

你想從'66任何日期的某個日期?你想使用日曆選擇它還是僅僅設置它? – lauda

回答

0

首先,這看起來像Java,JavaScript不,所以你的標籤可能是不正確的。

好的,讓我們一次一個地解決這個問題。 你會做手工什麼:

  1. 點擊輸入字段的「出生日期」
  2. 等待日曆來填充和變得可見
  3. 選擇一個月份
    你對此有一個類名菜單,每個選項都有一個值和文本從
  4. 選擇選擇一個年份 (步驟3用不同的類名和菜單選項的重複)
    因爲你的下拉菜單中有值這可能是更容易THA牛逼正好對齊到一年你想
  5. 你想從可見的日曆月「點擊」日期

關注的一些東西,可能是爲什麼你的代碼不能正常工作

driver.findElement(By.className("ui-datepicker-current-day")).click(); 

Select sel3=new Select(driver.findElement(By.cssSelector("selectDay"))); 

new Select((WebElement) sel3).selectByVisibleText("14"); 

這裏的第一行建議您嘗試單擊今天的日期,而不是第十四行

第二行和第三行看起來像您正在嘗試在日曆日進行選擇,但您無法在不是一個下拉選擇男人ü。你只能點擊它。另外,我很確定cssSelector(「selectDay」)不起作用。它不知道你是否在尋找一個ID,類名,標記名等等。我看到了幾個這樣的cssSelector問題。

同樣在這裏的第三行,你爲什麼要創建一個新的選擇到WebElement? 你應該可以做

sel3.selectByVisibleText("14"); 

DOB是ID嗎?你有一個與

driver.findElement(By.id("DOB")).click();; 

然後又

Select sel1=new Select(driver.findElement(By.cssSelector("DOB"))); 
相關問題