John smith
John smith

Reputation: 33

selenium cant find the element

im trying to write a code to automatically create a gmail using selenium. the item im trying to find is the firstname input in the url below:

https://accounts.google.com/signup/v2/webcreateaccount?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&flowName=GlifWebSignIn&flowEntry=SignUp

i used wait but it returns a TimeoutException error. also, say i want to use implicit wait, how can i do that?

thanks

class BotCreator:

    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname
        self.driver = webdriver.Firefox(executable_path=r'A:\Python Projects\The InstaBOT/geckodriver')

    def shutdown(self):
        self.driver.close

    def gmail_creator(self, n_bots):
        for n in range(n_bots):
            global email
            email = {}
            driver = self.driver
            wait = WebDriverWait(driver, 10)
            driver.get('https://www.google.com/intl/en-GB/gmail/about/')
            driver.find_element_by_xpath('//a[@title="Create an account"]').click()
            wait.until(ec.new_window_is_opened(driver.window_handles))
            after = driver.window_handles[1]
            driver.switch_to.window(after)
            element = wait.until(ec.element_to_be_clickable((By.XPATH, '//input[@id="firstName"]')))
            element.send_keys('awsd')

        return email

gmail_t = BotCreator('John', 'Hoffinsky')
gmail_t.gmail_creator(1)

Upvotes: 1

Views: 156

Answers (3)

KunduK
KunduK

Reputation: 33384

Once you click on the create Account button it opens a new window for user details.You need to Switch to that window to access the element.Try below code.

wait = WebDriverWait(driver, 10)
driver.get('https://www.google.com/intl/en-GB/gmail/about/')
driver.find_element_by_xpath('//a[@title="Create an account"]').click()
wait.until(ec.new_window_is_opened(driver.window_handles))
after=driver.window_handles[1]
driver.switch_to.window(after)
element = wait.until(ec.element_to_be_clickable((By.XPATH, '//input[@id="firstName"]')))
element.send_keys('awsd')

Browser snapshot:

enter image description here

Upvotes: 1

stacktome
stacktome

Reputation: 790

Is always good to use ID than XPath/CSS

Example

driver.find_element_by_id("FirstName").send_keys("email")

Upvotes: 0

terroruser
terroruser

Reputation: 23

There are actually several (4) elements with the same XPath on that page. Try to use a specific one with following XPath:

(//a[@title="Create an account"])[1]

Your XPath returns 4 elements with the Chrome extension ChroPath.


Regarding your question about implicit wait: You are already using implicit wait. The implicit wait setting tells Selenium to poll for the specified amount of time until an element is available. You can change the timeout if needed:

driver.implicitly_wait(15)

This link could be helpful.

Upvotes: 2

Related Questions