1

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 ?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
LearnToGrow
  • 1,656
  • 6
  • 30
  • 53
  • Okay, and then what? Your post [does not contain a question](/help/how-to-ask) right now, so there's nothing for anyone to answer. – Mike 'Pomax' Kamermans Feb 06 '23 at 18:38
  • @Mike'Pomax'Kamermans, I thought it is clear that the problem is how to send text to email – LearnToGrow Feb 06 '23 at 18:45
  • Describe what you set out to do, show the code you wrote to do that, then talk about what that code did, and how that was different from what you expected, and then also talk about what you already did in terms of (re)searching and debugging. So: please [edit] your post to talk us through your problem. SO is not a code writing service, so just "I want to know how to do X" is typically off topic (because the answer to that is "so search the web, common tasks have loads of tutorials online already") – Mike 'Pomax' Kamermans Feb 06 '23 at 19:01

2 Answers2

1

enter image description here

It should be input element to insert the value Not the label element that you have targeted..

Use element_to_be_clickable() instead of presence_of_element_located()

email_holder = wait_variable.until(E.element_to_be_clickable((By.ID, 'email')))
email_holder.send_keys("email")

if you still have the same issue, then set the window-size

options.add_argument("--window-size=1920,1080")
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Thanks for your answer but this not correct unless I miss something. This the first thing I've tried. If you use element with id=`email-label` and you print(element.text), you will see that this element hold the email not the element with id=`email`. To check your code, just print the email_holder.text and it will print empty text. – LearnToGrow Feb 06 '23 at 19:57
  • It is an input element then why are you expecting element.text returns some value. If you want to get value from input element you need to use element.get_attribute(‘value’) to print the value – KunduK Feb 06 '23 at 21:35
  • OP used the same ID where used the css selector if used css selector it is input#email. Which means node is input and Id is email. Hope you understood my answer wasn’t wrong at all. – KunduK Feb 06 '23 at 21:37
  • your answer is true. The problem is coming from my machine. The code is working on local machine but not on my server. – LearnToGrow Feb 06 '23 at 22:29
  • I have also suggested if you are using headless mode add window size as well.please add the window size as I describe and let me know the status. – KunduK Feb 06 '23 at 23:56
  • it works with and without the window size option – LearnToGrow Feb 07 '23 at 14:43
1

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Your code is working fine on my local machine. I think the main problem is because I am running the code on a server (no browser, options.add_argument('--headless')). – LearnToGrow Feb 06 '23 at 20:32
  • 1
    I do have `options.headless = True` as with latest builds _`headless`_ can't be set with `add_argument('--headless')` – undetected Selenium Feb 06 '23 at 20:33