问题
I am trying to write a login function. When I try to login into my yahoo account, I send the correct keys for my email address, which works, but then when I click "next" it 'misses' the click and instead clicks the banner which opens up some sort of advertisement be it travel related or Norton anti-security, or something. I've been working on this issue intermittently throughout the past week surfing and digging through forums before finally making my first post.
I am aware of the different ways of element selectors via css, id, class name, xpath, etc... I tried sleep(), implicit_wait(), things along those lines. I also tried something with wait until clickable with the expected conditions module from selenium.webdriver.
I've attached an image of what I have so far. My python version is up-to-date as are my selenium and chrome driver installations. I saw a similar post already up, but the OP didn't seem to be encountering my issue. (how to click on the yahoo sign in link using selenium web driver?)
I tried this as well, and it opens the advertisement; in my executions, Norton seems to be the most frequently appearing advertisement. (login to Yahoo using Python Selenium)
I've looked at the API documentation, but there doesn't seem to be any clear direction on what I could do. I've attached screenshots of what happens on the running of the script as well as the code I have.
I got it to work for some runs where I was able to go to the next form to send my password keys, but it happened randomly and inexplicably.
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()
Any help is well appreciated.
Norton advertisement instead of next form
回答1:
To send a character sequence to the Email address field and invoke click() on the button with text as Next you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using
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('my_username@yahoo.co.in') driver.find_element_by_css_selector("input#login-signin").submit()Using
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('my_username@yahoo.co.in') driver.find_element_by_xpath("//input[@id='login-signin']").submit()Browser Snapshot:
回答2:
Use WebDriverWait for the email field as well
wait = WebDriverWait(self.driver, 10)
wait.until(EC.visibility_of_element_located((By.ID, 'login-username'))).send_keys(email)
wait.until(EC.visibility_of_element_located((By.ID, 'login-signin'))).click()
来源:https://stackoverflow.com/questions/60276395/yahoo-sign-in-with-selenium-missing-click