Filling out “First Name” field of a signup page

2020-05-10 05:09发布

Problem i'm currently having is trying to fill out the First Name field out of a sign up page. I've managed to fill out the email name, and select the gender using selenium.

When I try to locate the First Name element using it's Xpath, I get an selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="bb1bda44-91c9-4668-8641-4f3bbbd0c6cd"]"} error

Code:

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

driver = selenium.webdriver.Chrome(executable_path='PATH')


def get_url(url):
    driver.get(url)
    driver.maximize_window()


def fill_data():
    # Find the signup button
    Wait(driver, 30).until(expected_conditions.presence_of_element_located
                           ((By.ID, 'signup-button'))).click()
    # Find the email name box
    email_name = Wait(driver, 30).until(expected_conditions.visibility_of_element_located
                                        ((By.XPATH, "/html/body/onereg-app/div/onereg-form/div/div/form/section/"
                                                    "section[1]/onereg-alias-check/fieldset/onereg-progress-meter"
                                                    "/div[2] "
                                                    "/div[2]/div/pos-input[1]/input")))
    # Enter the email name
    email_name.send_keys('Test')
    # Select gender
    driver.find_element_by_xpath('/html/body/onereg-app/div/onereg-form/div/div/form/section'
                                 '/section[2]/onereg-progress-meter/onereg-personal-info'
                                 '/fieldset/div/div/onereg-radio-wrapper[2]/pos-input-radio/label/i').click()
    # Find the first name box and fill out an account name
    driver.find_element_by_xpath('//*[@id="bb1bda44-91c9-4668-8641-4f3bbbd0c6cd"]')


get_url('https://www.mail.com/')
fill_data()

2条回答
做个烂人
2楼-- · 2020-05-10 05:39

The desired element is an dynamic element so to fill out the First 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://www.mail.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#signup-button"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[data-test='first-name-input']"))).send_keys("live for the hunt")
    
  • Using XPATH:

    driver.get("https://www.mail.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='signup-button']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@data-test='first-name-input']"))).send_keys("live for the hunt")
    
  • 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:

mail.com

查看更多
叼着烟拽天下
3楼-- · 2020-05-10 05:43

Few things observed: 1. you id is dynamic 2. You are performing find element with thee first name but you are not sending any text to it

Please find below solution:

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


# Open Chrome
driver = webdriver.Chrome('C:\New folder\chromedriver.exe')



def get_url(url):
    driver.get(url)
    driver.maximize_window()


def fill_data():
    # Find the signup button
    Wait(driver, 30).until(EC.presence_of_element_located((By.ID, 'signup-button'))).click()
    # Find the email name box
    email_name = Wait(driver, 30).until(EC.visibility_of_element_located
                                        ((By.XPATH, "/html/body/onereg-app/div/onereg-form/div/div/form/section/"
                                                    "section[1]/onereg-alias-check/fieldset/onereg-progress-meter"
                                                    "/div[2] "
                                                    "/div[2]/div/pos-input[1]/input")))
    # Enter the email name
    email_name.send_keys('Tes323t')
    # Select gender
    driver.find_element_by_xpath('/html/body/onereg-app/div/onereg-form/div/div/form/section'
                                 '/section[2]/onereg-progress-meter/onereg-personal-info'
                                 '/fieldset/div/div/onereg-radio-wrapper[2]/pos-input-radio/label/i').click()
    # Find the first name box and fill out an account name
    Wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@data-test='first-name-input']"))).send_keys('Tes323t')


get_url('https://www.mail.com/')
fill_data()
查看更多
登录 后发表回答