2015-11-04 162 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