需要使用selenium webdriver从gmail帐户创建页面的下拉列表中选择“男性”(我想使用SelectbyIndex方法)

时间:2017-03-25 05:51:35

标签: selenium selenium-webdriver

我在下面尝试了两个代码。两者都没有选择“男性”选项。谁能告诉我我在哪里犯错误。

在此网站中发布代码非常困难。这么多条件

My code:
    import org.openqa.selenium.Alert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.Select;
    import org.openqa.selenium.support.ui.WebDriverWait;
    public class webelements2 {
        public static void main(String[] args) throws InterruptedException 
        {
            System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");
            WebElement google = driver.findElement(By.xpath(".//*[@id='Gender']/div"));
            google.click();
            Select dropdown = new Select (driver.findElement(By.xpath(".//*[@id='Gender']/div")));
            dropdown.selectByIndex(1);
            }
    }



Even I used sendkeys method. But it didn't work for me

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class webelements2 {
    public static void main(String[] args) throws InterruptedException 
    {
        System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");
        WebElement google = driver.findElement(By.xpath(".//*[@id='Gender']/div"));
        google.sendKeys("Male");
        google.click();
    }

请建议我如何克服这个问题

2 个答案:

答案 0 :(得分:1)

您只能将Select()<select><option>元素一起使用。在目前的情况下,您只需在下拉列表中click(),然后click()选择所需的选项:

WebElement google = driver.findElement(By.xpath(".//*[@id='Gender']/div"));
        google.click();
WebElement option = (driver.findElement(By.xpath("//div[text()='Male']"));
        option.click();

您可能还需要等到option可点击:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement option = wait.until(elementToBeClickable(By.xpath("//div[text()='Male']")));
option.click();

答案 1 :(得分:0)

只需使用它,让我知道它是否适合您:

  

女性

driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":e")).click();
  

男性

driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":f")).click();
  

其他

driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":g")).click();
  

For Rather not say

driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":h")).click();

如果你需要注册gmail,我试过一次:

driver.manage().window().maximize();
driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");
driver.findElement(By.xpath(".//*[@id='FirstName']")).sendKeys("Name");
driver.findElement(By.xpath(".//*[@id='LastName']")).sendKeys("Last name");
driver.findElement(By.xpath(".//*[@id='GmailAddress']")).sendKeys("Email id");
driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("Password");
driver.findElement(By.xpath(".//*[@id='PasswdAgain']")).sendKeys("Password Again");
//Input the month
List<WebElement> month_dropdown = driver.findElements(By.xpath(".//*[@id='BirthMonth']/div"));
//iterate the list and get the expected month
Thread.sleep(3000);
for (WebElement month_ele:month_dropdown){
String expected_month = month_ele.getAttribute("innerHTML");
// Break the loop if match found

Thread.sleep(3000);
if(expected_month.equalsIgnoreCase("August")){
    month_ele.click();
    break;
}

driver.findElement(By.id("BirthMonth")).click();
driver.findElement(By.id(":3")).click();
driver.findElement(By.xpath(".//*[@id='BirthDay']")).sendKeys("14");
driver.findElement(By.xpath(".//*[@id='BirthYear']")).sendKeys("1988");

driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":e")).click();

driver.findElement(By.xpath(".//*[@id='RecoveryPhoneNumber']")).sendKeys("4694222863");
driver.findElement(By.xpath(".//*[@id='RecoveryEmailAddress']")).sendKeys("recovery email id");
driver.findElement(By.id("submitbutton")).click();
Thread.sleep(3000L);