0
driver = webdriver.Chrome()
driver.get("https://twitter.com/i/flow/signup")
driver.implicitly_wait(10)
setname = driver.find_element_by_name("name")
setname.click()
setname.send_keys("NAME SURNAME")
driver.implicitly_wait(10)
emailoption = driver.find_element_by_xpath("/html/body/div/div/div/div[2]/main/div/div/div/div[2]/div[2]/div/div[4]") #selenium can not find the element XPATH (I found it manually)
emailoption.click() #need to click in the element :)
driver.close()
  1. Selenium can not find the element XPATH (I found it manually) ---Use email instead---.
  2. Tried to find this element other ways... same result.
  3. Maybe there is another way to click in it?
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried `$x("/html/body/div/div/div/div[2]/main/div/div/div/div[2]/div[2]/div/div[4]")` in a console and ended up with an empty array. When I navigate to that element and copy its xpath, I get this `/html/body/div/div/div/div[1]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div[4]/span`... – msanford Jul 30 '20 at 02:45

5 Answers5

1

Add a Selenium expected condition (element_to_be_clickable) to your code and use relative XPath. To input your name, click on the link and input your email, you can use :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@autocomplete='name']"))).send_keys('name')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@autocomplete='tel']/following::span[1]']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@autocomplete='email']"))).send_keys('email')

Be sure to add the following imports :

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

If it still fails you can use Javascript :

name = "your_name"
email = "your_email"
elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@autocomplete='name']")))
self.driver.execute_script("arguments[0].setAttribute('value', '" + name +"')", elem)
elem2 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@autocomplete='tel']/following::span[1]']")))
self.driver.execute_script("arguments[0].click();", elem2)
elem3 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@autocomplete='email']")))
self.driver.execute_script("arguments[0].setAttribute('value', '" + email +"')", elem3)
E.Wiest
  • 5,425
  • 2
  • 7
  • 12
1

The elements within Twitter Signup page are React elements. So to send a character sequence to the Name field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get("https://twitter.com/i/flow/signup")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='name']"))).send_keys("Manoel Augusto")
    
  • Using XPATH:

    driver.get("https://twitter.com/i/flow/signup")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='name']"))).send_keys("Manoel Augusto")
    
  • 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:

twitter_name


Reference

You can find a detailed relevant discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I ran your code on my computer and It worked perfectly, finding the email option and clicking on it. I also found the same xpath you provided, have you tried running your program in a debugger, and What IDE/text editor are you using? Also, are you leaving the window open when you are running your program? If you close the window it will throw an error. If you have any more details on what is happening when you run your code let me know I'd love to help.

matlac
  • 23
  • 3
  • I am using PyCharm, and keep the window opened. It is a NoSuchElementException error . Sorry for the inconvenience and thanks for your attention :) – Manoel Augusto Jul 30 '20 at 03:00
0

Try below code -

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome()

action = ActionChains(driver)
wait = WebDriverWait(driver, 20)

driver.get('https://twitter.com/i/flow/signup')

time.sleep(5)  # Add wait here so that twitter login page will load.

NameElement = driver.find_element_by_xpath("//input[@name='name']")
action.move_to_element(NameElement).click().perform()
NameElement.send_keys("Hello")

EmailOption = driver.find_element_by_xpath('//span[text()="Use email instead"]/parent::div')
EmailOption.click()

driver.close()
Swaroop Humane
  • 1,770
  • 1
  • 7
  • 17
0

Try below code :

wait = WebDriverWait(driver, 10)
driver.get("https://twitter.com/i/flow/signup")
driver.implicitly_wait(10)
setname = driver.find_element_by_name("name")
setname.click()
setname.send_keys("NAME SURNAME")
driver.implicitly_wait(10)
element1 = wait.until(EC.element_to_be_clickable((By.XPATH, "//body//div[4]")))
element1.click()

Output::

enter image description here

SeleniumUser
  • 4,065
  • 2
  • 7
  • 30