【问题标题】:creating gmail account using selenium使用 selenium 创建 gmail 帐户
【发布时间】:2016-02-05 06:31:38
【问题描述】:

无法选择出生日期的月份。

我使用的代码是:

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 案例。但无法设置任何月份。

请告诉我解决方案。

【问题讨论】:

    标签: java selenium automation automated-tests


    【解决方案1】:

    你可以把它封装在一个函数中

    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
    

    【讨论】:

    • 对不起 Jeff ,但这样也无法选择月份。对你有用吗??
    • 性别选择发生了同样的事情。
    • 是的,代码对我有用。我成功运行了几次。
    【解决方案2】:

    您可以使用键盘事件替换鼠标。

    WebElement birthMonth = driver.findElement(By.id("BirthMonth"));
    birthMonth.click();
    Actions action = new Actions(driver);
    action.sendKeys(Keys.DOWN).sendKeys(Keys.ENTER).perform();
    

    【讨论】:

      【解决方案3】:
      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;
              }
          }
      

      【讨论】:

        【解决方案4】:

        我们可以直接使用sendKeys

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

        【讨论】:

          【解决方案5】:

          这不是下拉值,您必须单击下拉箭头,然后单击必须从脚本传递的任何值。

          代码如下:

          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(); 
          

          这是生日月份的可行代码

          请在下面找到相同类型问题的链接

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

          【讨论】:

            猜你喜欢
            • 2023-02-17
            • 2022-12-18
            • 2013-07-30
            • 1970-01-01
            • 1970-01-01
            • 2020-07-29
            • 1970-01-01
            • 1970-01-01
            • 2021-07-30
            相关资源
            最近更新 更多