from selenium import webdriver
from time import sleep
fname="Misuzu"
lname="Fusuzu"
number=987654321
driver=webdriver.Chrome()
driver.get('https://contacts.google.com/')
email=driver.find_element_by_id("identifierId")
email.send_keys('[email protected]')
psw='123456789'
submit=driver.find_element_by_id("identifierNext")
submit.click()
sleep(2)
password=driver.find_element_by_name("password")
password.send_keys(psw)
submit=driver.find_element_by_id("passwordNext")
submit.click()
sleep(2)
create_contact=driver.find_element_by_xpath("//div[@class='aU2Vdf'][button/@class='VfPpkd-BIzmGd VfPpkd-BIzmGd-OWXEXe-X9G3K bgpk6e']")
create_contact.click()
sleep(10)
#This is the part frome where my code stops working and raises the element not interactable error inspite of it being the part where I need to input my name.
driver.switch_to.active_element
fnameins=driver.find_element_by_xpath("//div[@class='x3wWge'][//div[@class='Xb9hP'][input/@class='whsOnd zHQkBf']]")
fnameins.click()
lnameins=driver.find_element_by_xpath("//input[@class='whsOnd zHQkBf']")
lnameins.send_keys(lname)
numins=driver.find_element_by_xpath("//input[@class='whsOnd zHQkBf']")
numins.send_keys(number)
save=driver.find_element_by_xpath("//input[@class='VfPpkd-vQzf8d']")
save.click()
sleep(2)
driver.quit()
"//div[@class='x3wWge'][//div[@class='Xb9hP'][input/@class='whsOnd zHQkBf']]"
是错误的xpath,它返回错误的Web元素
不要使用动态生成的@class属性的值,例如aU2Vdf(这些值很快就会改变)
我知道我没有回答,但是尝试搜索正确的xpaths
根据您的代码示例,我添加了几行与模式框进行交互,以帮助您入门。不过,您需要填补其余字段的空白。我已经测试了这段代码,并成功地将键发送到模式框元素。
driver=webdriver.Chrome()
driver.get('https://contacts.google.com/')
email=driver.find_element_by_id("identifierId")
email.send_keys('[email protected]')
psw='123456789'
submit=driver.find_element_by_id("identifierNext")
submit.click()
sleep(2)
password=driver.find_element_by_name("password")
password.send_keys(psw)
submit=driver.find_element_by_id("passwordNext")
submit.click()
sleep(2)
create_contact=driver.find_element_by_xpath("//div[@class='aU2Vdf'][button/@class='VfPpkd-BIzmGd VfPpkd-BIzmGd-OWXEXe-X9G3K bgpk6e']")
create_contact.click()
# wait for first name field to exist, then send text to it
first_name = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//input[@aria-label='First name']")))
first_name.send_keys("test")
# enter more fields here
# click save
driver.find_element_by_xpath("//span[text()='Save']").click()
# if the above statement does not work, you can try the JS click:
#save_button = driver.find_element_by_xpath("//span[text()='Save']")
#driver.execute_script("arguments[0].click();", save_button)
# click cancel
driver.find_element_by_xpath("//span[text()='Cancel']").click()
# if the above statement does not work, you can try the JS click:
#save_button = driver.find_element_by_xpath("//span[text()='Cancel']")
#driver.execute_script("arguments[0].click();", save_button)
[在将文本发送到模式之前,请注意我如何调用WebDriverWait
。我建议您也对脚本的其余部分也采用这种方法,而不是sleep(2)
,您可以在等待存在的特定元素上调用WebDriverWait
。这样,您将永远不会等待不必要的时间,并且如果页面加载所需的时间比正常时间长,脚本也不会失败。