gpt4 book ai didi

python - 无法使用 selenium python 访问网站上的 Google Alert 登录

转载 作者:行者123 更新时间:2023-12-01 07:58:00 25 4
gpt4 key购买 nike

我正在尝试创建一个脚本,在其中使用我的谷歌帐户登录网站,但我遇到了一个无法解决的重大问题。

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 登录的弹出窗口。之后,我收到一条错误消息,指出未找到该元素。任何帮助将不胜感激,谢谢大家!

最佳答案

感谢@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()

感谢大家的帮助!

关于python - 无法使用 selenium python 访问网站上的 Google Alert 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55866101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com