我正在尝试填写Gmail创建页面中的表格。但由于某种原因,我的司机找不到代码。我已经使用了所有的选项,路径,id,名称,类名,但没有任何工作
chrome_driver = './chromedriver'
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
driver.get('https://www.google.com/intl/nl/gmail/about/#')
try:
print('locating create account button')
create_account_button = driver.find_element_by_class_name('h-c-button')
except:
print("error, couldn't find create account button")
try:
create_account_button.click()
print('navigating to creation page')
except:
print('error navigating to creation page')
time.sleep(15)
first_name_form = driver.find_element_by_class_name('whsOnd zHQkBf')
(睡眠只是暂时的,为了确保它完全加载,我知道它没有效率(
这是Gmail页面的链接:https://accounts.google.com/signup/v2/webcreateaccount?service=mail&continue=https%3A%2F%2Mail.google.com%2Mail%2F%3Fpc%3Topnav-about-n-en&flowName=GlifWebSignIn&flowEntry=注册
这是我得到的错误:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"css
selector","selector":".whsOnd zHQkBf"}
(Session info: chrome=81.0.4044.129)
感谢您的帮助
我发现了您的错误,我有解决方案。让我先给你确定一下这个问题。当您单击"创建新帐户"时,将打开一个新窗口。但你的机器人仍然不值得注意的是,你位于第一个窗口(在你点击第一个按钮以创建帐户的窗口中(。因此,机器人程序正在尝试查看是否有"名字"输入。这就是失败的原因。因此,解决方案是必须更改要指定的窗口。你可以这样做的方式是写在代码块里面。
代码
from selenium import webdriver
import time
path = '/home/avionerman/Documents/stack'
driver = webdriver.Firefox(path)
driver.get('https://www.google.com/intl/nl/gmail/about/#')
try:
print('locating create account button')
create_account_button = driver.find_element_by_class_name('h-c-button')
except:
print("error, couldn't find create account button")
try:
create_account_button.click()
print('navigating to creation page')
except:
print('error navigating to creation page')
time.sleep(15)
# Keeping all the windows into the array named as handles
handles = driver.window_handles
# Keeping the size of the array in order to know how many windows are open
size = len(handles)
# Switch to the second opened window (id:1)
driver.switch_to.window(handles[1])
# Print the title of the current page in order to validate if it's the proper one
print(driver.title)
time.sleep(10)
first_name_input = driver.find_element_by_id('firstName')
first_name_input.click()
first_name_input.send_keys("WhateverYouWant")
last_name_input = driver.find_element_by_id('lastName')
last_name_input.click()
last_name_input.send_keys("WhateverYouWant2")
username_input = driver.find_element_by_id('username')
username_input.click()
username_input.send_keys('somethingAsAUsername')
pswd_input = driver.find_element_by_name('Passwd')
pswd_input.click()
pswd_input.send_keys('whateveryouwant')
pswd_conf_input = driver.find_element_by_name('ConfirmPasswd')
pswd_conf_input.click()
pswd_conf_input.send_keys('whateveryouwant')
time.sleep(20)
所以,如果你转到第21行,你会看到我有一些评论,以便告诉你这些行(从第21行到第31行(在做什么。
此外,我为你插入了所有需要的代码(名字、姓氏等(。你只需要找到创建按钮(最后一个(。
注意:在这种情况下,尽量使用id,而不是像我为您所做的那样使用类名(当id清晰且唯一时(。