无法使用硒python在网站上使用Google警报访问登录

时间:2019-04-26 10:53:50

标签: python selenium selenium-webdriver element

我正在尝试创建一个脚本,在该脚本中我使用自己的Google帐户登录到一个网站,但是遇到了一个主要问题,无法解决。

import selenium
from selenium import webdriver

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("/Users/vagif/Downloads/chromedriver")
driver.get('https://passport.alibaba.com/icbu_login.htm?return_url=https%3A%2F%2Fwww.alibaba.com%2F')
driver.implicitly_wait(3)
google = driver.find_element_by_xpath('//a[@attr-type="google"]')
google.click()

time.sleep(1)
actions = ActionChains(driver)
actions.key_down(Keys.COMMAND + '~').perform()
time.sleep(1)
element = driver.find_element_by_xpath('//input[@type="email"]')

element.send_keys('leine.tee1@gmail.com')
time.sleep(1)
next = driver.find_element_by_class_name('RveJvd').click()
time.sleep(1)
password = driver.find_element_by_class_name('whsOnd').send_keys('***password***')
next = driver.find_element_by_class_name('RveJvd').click()

我的问题是代码可以正常工作,直到出现带有Google弹出窗口的登录信息。之后,我收到一条错误消息,指出未找到该元素。任何帮助将不胜感激,谢谢大家!

1 个答案:

答案 0 :(得分:1)

感谢@ demouser123和this link我能够解决问题。

通过添加以下代码:

window_before = driver.window_handles[0]
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)

我成功地能够切换标签。这是我的完整代码:

import selenium
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("/Users/vagif/Downloads/chromedriver")
driver.get('https://passport.alibaba.com/icbu_login.htm?return_url=https%3A%2F%2Fwww.alibaba.com%2F')
driver.implicitly_wait(3)
google = driver.find_element_by_xpath('//a[@attr-type="google"]')
google.click()

window_before = driver.window_handles[0]
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)
time.sleep(1)

time.sleep(1)
element = driver.find_element_by_xpath('//input[@type="email"]')

element.send_keys('leine.tee1@gmail.com')
time.sleep(1)
next = driver.find_element_by_class_name('RveJvd').click()
time.sleep(2)
password = driver.find_element_by_class_name('whsOnd').send_keys('****password')
next = driver.find_element_by_class_name('RveJvd').click()

谢谢大家的帮助!