我正在尝试编写一个登录功能。当我尝试登录我的雅虎帐户时,我发送了我的电子邮件地址的正确 key ,这有效,但是当我单击“下一步”时,它“错过”了单击,而是单击了打开某种广告的横幅它与旅行相关或诺顿防安全或其他东西。在过去的一周里,我一直在断断续续地研究这个问题,在论坛上冲浪和挖掘,最后才发表了我的第一篇文章。
我知道元素选择器通过 css、id、类名、xpath 等的不同方式...我尝试了 sleep()、implicit_wait() 等类似方法。我还尝试了一些等待,直到可以使用 selenium.webdriver 中的预期条件模块进行点击。
我已附上迄今为止所拥有的图像。我的 python 版本是最新的,我的 selenium 和 chrome 驱动程序安装也是最新的。我已经看到类似的帖子了,但OP似乎没有遇到我的问题。 (how to click on the yahoo sign in link using selenium web driver?)
我也试过了,它打开了广告;在我执行的广告中,诺顿似乎是出现频率最高的广告。 (login to Yahoo using Python Selenium)
我查看了 API 文档,但似乎没有任何明确的方向说明我可以做什么。我已附上脚本运行时发生的情况以及我拥有的代码的屏幕截图。
我让它在一些运行中工作,我能够转到下一个表单来发送我的密码 key ,但它随机且莫名其妙地发生。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from login import password, email
from time import sleep
class yahoo():
def __init__(self):
# INITIALIZE CHROME WEBDRIVER
self.driver = webdriver.Chrome()
self.driver.maximize_window()
def login(self):
# OPEN BROWSER TO LOGIN PAGE AND MAXIMIZE
self.driver.get("https://login.yahoo.com/config/login?.\
src=fpctx&.intl=us&.lang=en-US&.done=https://www.yahoo.com")
# LOGIN ACTIONS
{# 1. send email address and click next
self.driver.find_element_by_id('login-username').send_keys(email)
element = WebDriverWait(self.driver, 10).until(\
EC.presence_of_element_located((By.ID, "login-signin")))
element.click()
# 2. send password and click sign in
self.driver.find_element_by_xpath('//*[@id="login-passwd"]').send_keys(password)
self.driver.find_element_by_id('login-signin').click()}`enter code here
x = yfscreeners()
x.login()
非常感谢任何帮助。
最佳答案
要将字符序列发送到电子邮件地址字段并在按钮上调用click()
,并将文本设置为Next,您需要诱导对于 element_to_be_clickable()
WebDriverWait,您可以使用以下任一 Locator Strategies :
使用
css_selector
:from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get('https://login.yahoo.com') WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.phone-no#login-username"))).send_keys('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a3723052f293f28343b373f1a233b323535743935743334" rel="noreferrer noopener nofollow">[email protected]</a>') driver.find_element_by_css_selector("input#login-signin").submit()
使用
xpath
:from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get('https://login.yahoo.com') WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='phone-no ' and @id='login-username']"))).send_keys('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3aeba9cb6b0a6b1ada2aea683baa2abacaceda0acedaaad" rel="noreferrer noopener nofollow">[email protected]</a>') driver.find_element_by_xpath("//input[@id='login-signin']").submit()
浏览器快照:
关于python - 使用 Selenium 登录雅虎,缺少点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60276395/