MarWo22
MarWo22

Reputation: 51

Can't find element Selenium

I am trying to fill in the forms in the Gmail creation page. But for some reason, my driver can't find the code. I've used all the options, path, id, name, class name, but nothing works

chrome_driver = './chromedriver'   
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)


driver.get('https://www.google.com/intl/nl/gmail/about/#')

try:
    print('locating create account button')
    create_account_button = driver.find_element_by_class_name('h-c-button')
except:
    print("error, couldn't find create account button")
try:
    create_account_button.click()
    print('navigating to creation page')
except:
    print('error navigating to creation page')
time.sleep(15)

first_name_form = driver.find_element_by_class_name('whsOnd zHQkBf')

(the sleep is just temporary, to make sure it loads completely, I know it's not efficient)

here's the link to the Gmail page: https://accounts.google.com/signup/v2/webcreateaccount?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Fpc%3Dtopnav-about-n-en&flowName=GlifWebSignIn&flowEntry=SignUp

This is the error I'm getting:

Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"css 
selector","selector":".whsOnd zHQkBf"}
  (Session info: chrome=81.0.4044.129)

Thank you for your help

Upvotes: 2

Views: 85

Answers (1)

dpapadopoulos
dpapadopoulos

Reputation: 1846

I found your error and I have the solution for you. Let me first determine to you the problem. When you click on the 'Create New Account' a new window is opening. BUT your bot is still undestanding that you are located in the first window (in the one that you click on the first button in order to create the account). Thus, the bot is trying to see if there is a First Name input. That's why is failing. So, the solution is that you have to change the window that you want to specify. The way you can do it is written inside the code block.

CODE

from selenium import webdriver
import time

path = '/home/avionerman/Documents/stack'
driver = webdriver.Firefox(path)

driver.get('https://www.google.com/intl/nl/gmail/about/#')

try:
    print('locating create account button')
    create_account_button = driver.find_element_by_class_name('h-c-button')
except:
    print("error, couldn't find create account button")
try:
    create_account_button.click()
    print('navigating to creation page')
except:
    print('error navigating to creation page')
time.sleep(15)

# Keeping all the windows into the array named as handles
handles = driver.window_handles

# Keeping the size of the array in order to know how many windows are open
size = len(handles)

# Switch to the second opened window (id:1)
driver.switch_to.window(handles[1])

# Print the title of the current page in order to validate if it's the proper one
print(driver.title)

time.sleep(10)



first_name_input = driver.find_element_by_id('firstName')
first_name_input.click()
first_name_input.send_keys("WhateverYouWant")

last_name_input = driver.find_element_by_id('lastName')
last_name_input.click()
last_name_input.send_keys("WhateverYouWant2")

username_input = driver.find_element_by_id('username')
username_input.click()
username_input.send_keys('somethingAsAUsername')

pswd_input = driver.find_element_by_name('Passwd')
pswd_input.click()
pswd_input.send_keys('whateveryouwant')

pswd_conf_input = driver.find_element_by_name('ConfirmPasswd')
pswd_conf_input.click()
pswd_conf_input.send_keys('whateveryouwant')

time.sleep(20)

So, if you will go to line 21 you will see that I have some comments in order to tell you what these lines (from 21 until 31) are doing.

Also, I inserted all the needed code for you (first name, last name, et.c). You only have to locate the creation button (last one).

Note: Try to use ids in such cases and not class names (when the ids are clear and unique) as I already did for you.

Upvotes: 1

Related Questions