使用 selenium 创建 gmail 帐户
creating gmail account using selenium
无法Select出生日期的月份。
我使用的代码是:
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();
我也尝试使用 DropDownList 案例。但无法设置任何月份。
请告诉我解决方案。
您可以将其包装在一个函数中
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
您可以使用键盘事件代替鼠标。
WebElement birthMonth = driver.findElement(By.id("BirthMonth"));
birthMonth.click();
Actions action = new Actions(driver);
action.sendKeys(Keys.DOWN).sendKeys(Keys.ENTER).perform();
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;
}
}
我们可以直接使用sendKeys
:
driver.findElement(By.xpath(".//*[@id='BirthMonth']/div[1]")).sendKeys("July");
它不是下拉值,您必须单击下拉箭头,然后单击您必须从脚本传递的任何值。
代码如下:
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();
这是生日的可用代码
请在下面找到 link 相同类型的问题
Not able to select the value from drop down list by using Select method in Selenium
无法Select出生日期的月份。
我使用的代码是:
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();
我也尝试使用 DropDownList 案例。但无法设置任何月份。
请告诉我解决方案。
您可以将其包装在一个函数中
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
您可以使用键盘事件代替鼠标。
WebElement birthMonth = driver.findElement(By.id("BirthMonth"));
birthMonth.click();
Actions action = new Actions(driver);
action.sendKeys(Keys.DOWN).sendKeys(Keys.ENTER).perform();
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;
}
}
我们可以直接使用sendKeys
:
driver.findElement(By.xpath(".//*[@id='BirthMonth']/div[1]")).sendKeys("July");
它不是下拉值,您必须单击下拉箭头,然后单击您必须从脚本传递的任何值。
代码如下:
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();
这是生日的可用代码
请在下面找到 link 相同类型的问题
Not able to select the value from drop down list by using Select method in Selenium