填写“名字"注册页面的字段 [英] Filling out "First Name" field of a signup page
问题描述
我当前遇到的问题是尝试在注册页面中填写名字字段.我设法填写了电子邮件名称,并使用硒选择了性别.
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.
当我尝试使用Xpath来定位First Name元素时,出现selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="bb1bda44-91c9-4668-8641-4f3bbbd0c6cd"]"}
错误
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
代码:
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()
推荐答案
所需的元素是动态元素,因此必须填写 First 字段,以诱导 WebDriverWait element_to_be_clickable()
,则可以使用以下任一定位器策略:
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:
-
使用
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")
使用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:
这篇关于填写“名字"注册页面的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!