2015-11-04 161 views
-4

無法選擇出生日期的月份。我使用使用硒創建Gmail帳戶

代碼是:使用DropDownList的情況下

driver.findElement(By.xpath(".//*[@id = 'BirthMonth']/div")).click(); 

Thread.sleep(3000);  
WebElement months = driver.findElement(By.xpath(".//[@id='BirthMonth']/div[2]/div[@id=':1']")); 

Thread.sleep(2000); 

months.click(); 

我也試過。但不能設置任何月份。

請說出我的解決方案。

回答

0

您可以在一個功能

public void selectBirthMonth(int monthIndex) 
{ 
    driver.findElement(By.cssSelector("#BirthMonth > div")).click(); 
    driver.findElements(By.cssSelector("#BirthMonth > div.goog-menu > div.goog-menuitem")).get(monthIndex - 1).click(); 
} 

包裝這件事,然後調用它像

selectBirthMonth(9); // 1 = January 
+0

對不起傑夫的,但這也無法選擇月份。它爲你工作? – Shirsh

+0

與性別選擇發生同樣的事情。 – Shirsh

+0

是的,代碼爲我工作。我成功地跑了好幾次。 – JeffC

0

您可以使用鍵盤事件代替鼠標。

WebElement birthMonth = driver.findElement(By.id("BirthMonth")); 
birthMonth.click(); 
Actions action = new Actions(driver); 
action.sendKeys(Keys.DOWN).sendKeys(Keys.ENTER).perform(); 
0
WebElement month = wd.findElement(By.xpath("//[@id=\"BirthMonth\"]/div[1]")); 
month.click(); 
Thread.sleep(3000); 
//wd.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 

//fetch months from the dropdown and store it in a list 
List<WebElement> month_dropdown = wd.findElements(By.xpath("//[@id=\"BirthMonth\"]/div[2]/div/div")); 
    //iterate the list and get the expected month 
    for (WebElement month_ele:month_dropdown){ 
    String expected_month = month_ele.getAttribute("innerHTML"); 
    // Break the loop if match found 
    if(expected_month.equalsIgnoreCase("August")){ 
     month_ele.click(); 
     break; 
     } 
    } 
1

我們可以直接使用sendKeys

driver.findElement(By.xpath(".//*[@id='BirthMonth']/div[1]")).sendKeys("July"); 
0

這是降不下來的值,你必須點擊下拉箭頭,然後單擊您必須從傳遞任何價值腳本。

代碼如下:

System.setProperty("webdriver.chrome.driver", "E:/software and tools/chromedriver_win32/chromedriver.exe"); 
WebDriver driver= new ChromeDriver(); 


//FirefoxDriver driver= new FirefoxDriver(); 
driver.manage().window().maximize(); 
driver.get("https://accounts.google.com/SignUp"); 
Thread.sleep(5000); 
driver.findElement(By.xpath(".//*[@id='BirthMonth']/div[1]/div[2]")).click(); 
driver.findElement(By.xpath(".//*[@id=':7']/div")).click(); 

它是Birthmonth

可行的代碼,請在下面找到鏈接,同一類型的問題

Not able to select the value from drop down list by using Select method in Selenium