How to select from available usernames while creating Gmail account using Selenium when the username was chosen is already taken?

driver.findElement(By.id("FirstName")).sendKeys("7 Grams");

I wanted steps after this: I am looking to select the first suggested username that appears in the available usernames.


In Selenium python: first we find email input and send keys, when it's wrong we use try to find error message and except new email address with e.g. random int added. For username similar with another selector.

   email = self.driver.find_element_by_css_selector("input#GmailAddress")
   next_step = self.driver.find_element_by_css_selector("input#submitbutton")
       error = self.driver.find_element_by_css_selector("span.errormsg")
   except Exception:
       email2 = self.driver.find_element_by_css_selector("input#GmailAddress")
       rand = random.randint(1, 10)
       randS = str(rand)

Now in gmail it is no username suggestion Now it looks like this.

  • Hi, thank you so much. Could you assist me in writing this in Selenium Java? I am looking to select the first available username that Gmail is suggesting when the username we chose is already taken. – VarzTester Jun 5 '17 at 23:44
  • If you want i can rewrite this code in Java. Please try to to trigger situation with suggesting usernames and make screen of html code. – siutex Jun 7 '17 at 12:52
  • WebElement gmailUsername = driver.findElement(By.cssSelector("input#GmailAddress")); gmailUsername.sendKeys("abcabc"); WebElement submitButton = driver.findElement(By.cssSelector("input#submitbutton")); submitButton.click(); try{ WebElement errorMessage = driver.findElement(By.cssSelector("span.errormsg")); WebElement gmailUsername2 = driver.findElement(By.cssSelector("div.username-suggestions>a:first-of-type")); gmailUsername2.click(); submitButton.click(); } catch(WebDriverException e){ e.printStackTrace(); } – siutex Jun 8 '17 at 5:22
  • String error = driver.findElement(By.xpath(".//*[@id='errormsg_0_GmailAddress']")).getText(); if(error.equals("That username is taken. Try another.")) { List<WebElement> available = driver.findElements(By.xpath(".//*[@id='username-suggestions']/a")); System.out.println("Size "+available.size()); for(int i=1;i<=available.size();i++) { WebElement e = driver.findElement(By.xpath(".//*[@id='username-suggestions']/a["+i+"]")); e.click(); break; } } – VarzTester Jun 8 '17 at 22:55
  • I have tried this and is working....Thank you – VarzTester Jun 8 '17 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.