Search code examples
pythonseleniumxpathcss-selectorswebdriverwait

How to avoid click intercept while sending text to email field using Selenium in headless mode


I want to connect website. I write the following code:

from time import sleep
from fake_useragent import UserAgent
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E


from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('--no-sandbox')
options.add_argument('--headless')
user_agent = UserAgent().random
options.add_argument(f'user-agent={self.user_agent}')
options.add_argument('--disable-infobars')
options.add_argument('--disable-dev-shm-usage') 

driver = webdriver.Chrome(driver_path, options=self.options) 
wait_time = 10
wait_variable = W(self.driver, self.wait_time)  
driver.get("https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor")
sleep(5)  
email_holder = wait_variable.until(E.presence_of_element_located((By.ID, 'email-label')))
# this does not work
# I tried to focus on, click on but nothing is working.
# it looks that another element receive the click
# email_holder.click()
email_holder.send_keys("email")

My question is how to focus and send text to email_holder ?


Solution

  • To send a character sequence to the Email Address field within the loginpage you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      options = Options()
      options.headless = True
      options.add_argument("start-maximized")
      s = Service('C:\\BrowserDrivers\\chromedriver.exe')
      driver = webdriver.Chrome(service=s, options=options)
      driver.get('https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#email"))).send_keys("LearnToGrow@stackoverflow.com")
      driver.save_screenshot("email.png")
      
    • Using XPATH:

      options = Options()
      options.headless = True
      options.add_argument("start-maximized")
      s = Service('C:\\BrowserDrivers\\chromedriver.exe')
      driver = webdriver.Chrome(service=s, options=options)
      driver.get('https://app.wordtune.com/account/login?product=write&platform=editor&afterAuthRedirect=%2Feditor')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='email']"))).send_keys("LearnToGrow@stackoverflow.com")
      driver.save_screenshot("email.png")
      
    • Note: You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
    • Browser snapshot:

    email.png