Python 填写「;“名字”;注册页的字段
我目前遇到的问题是试图从注册页面中填写名字字段。我已经设法填写了电子邮件名称,并使用selenium选择了性别 当我尝试使用Xpath来定位First Name元素时,会得到一条Python 填写「;“名字”;注册页的字段,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,我目前遇到的问题是试图从注册页面中填写名字字段。我已经设法填写了电子邮件名称,并使用selenium选择了性别 当我尝试使用Xpath来定位First Name元素时,会得到一条selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“method”:“Xpath”,“selector”:“/*[@id=“bb1bda44-91c9-4668-8641-4f3bbbd0c6cd”}/code>错误 代码: 观察到的
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“method”:“Xpath”,“selector”:“/*[@id=“bb1bda44-91c9-4668-8641-4f3bbbd0c6cd”}/code>错误
代码:
观察到的事情很少:
1.你的id是动态的
2.您正在使用您的名字执行find元素,但没有向它发送任何文本
请找到以下解决方案:
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()
所需元素是一个动态元素,因此要填写First Name字段,您必须引导WebDriverWait使元素可点击()
,并且您可以使用以下任一选项:
- 使用
CSS\u选择器
:
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")
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
- 浏览器快照:
我猜那些id
值是随机生成的。尝试使用另一个不变的属性,例如data test=“first name input”
。此外,无论出于何种原因,您都无法完成此过程,因为表单末尾有请求的验证码。你可能想重新考虑你的努力。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC