Reputation: 33
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:
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
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:
Upvotes: 1
Reputation: 790
Is always good to use ID than XPath/CSS
Example
driver.find_element_by_id("FirstName").send_keys("email")
Upvotes: 0
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