我正在努力使用 Python 和 Selenium 登录雅虎网络邮件。 我创建了一个代码并在 Google recaptcha 演示上进行了测试,它可以工作,但在雅虎邮件上尝试时出现错误。
import zipfile
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep
from random import randint
import pydub
import urllib
from speech_recognition import Recognizer, AudioFile
import os
from selenium.webdriver.chrome.service import Service
import random
import string
chrome_options = webdriver.ChromeOptions()
chrome_options.add_extension(plugging)
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument('--user-data-dir=' + ProfilePath)
path = os.path.abspath(os.getcwd())
tt = Service(path + "\\chromedriver.exe")
# tt = Service("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")
driver = webdriver.Chrome(service=tt, options=chrome_options)
# driver.get("https://www.google.com/recaptcha/api2/demo")
driver.get("https://login.yahoo.com/")
sleep(4)
ema = driver.find_element(By.XPATH, '//*[@id="login-username"]')
ema.send_keys('[email protected]')
sleep(2)
ema.send_keys(Keys.ENTER)
sleep(4)
#
frames = driver.find_elements(By.TAG_NAME, "iframe")
driver.switch_to.frame(frames[0])
sleep(randint(2, 4))
checkbo = driver.find_element(By.XPATH, '//*[@id="recaptcha-anchor"]/div[1]')
# checkbo = driver.find_element(By.CLASS_NAME, "recaptcha-checkbox-border")
checkbo.click()
sleep(randint(2, 4))
driver.switch_to.default_content()
frames = driver.find_element(By.XPATH,
"/html/body/div[2]/div[4]").find_elements(By.TAG_NAME, "iframe")
sleep(randint(2, 4))
driver.switch_to.default_content()
frames = driver.find_elements(By.TAG_NAME, "iframe")
driver.switch_to.frame(frames[-1])
driver.find_element(By.ID, "recaptcha-audio-button").click()
driver.switch_to.default_content()
frames = driver.find_elements(By.TAG_NAME, "iframe")
driver.switch_to.frame(frames[-1])
sleep(randint(2, 4))
driver.find_element(By.XPATH, "/html/body/div/div/div[3]/div/button").click()
try:
src = driver.find_element(By.ID, "audio-source").get_attribute("src")
print(src)
urllib.request.urlretrieve(src, path + "\\audio.mp3")
sound = pydub.AudioSegment.from_mp3(
path + "\\audio.mp3").export(path + "\\audio.wav", format="wav")
recognizer = Recognizer()
recaptcha_audio = AudioFile(path + "\\audio.wav")
with recaptcha_audio as source:
audio = recognizer.record(source)
text = recognizer.recognize_google(audio, language="en-EN")
print(text)
inputfield = driver.find_element(By.ID, "audio-response")
inputfield.send_keys(text.lower())
inputfield.send_keys(Keys.ENTER)
sleep(10)
print("Success")
driver.quit()
except NameError:
print("Failed")
print(NameError)
driver.quit()
你能帮我解决这个问题吗?
[![在此处输入图像描述][1]][1]
我的代码中的这一部分不起作用,它是关于单击复选框(我不是机器人)
frames = driver.find_elements(By.TAG_NAME, "iframe")
driver.switch_to.frame(frames[0])
sleep(randint(2, 4))
checkbo = driver.find_element(By.XPATH, '//*[@id="recaptcha-anchor"]/div[1]')
# checkbo = driver.find_element(By.CLASS_NAME, "recaptcha-checkbox-border")
checkbo.click()
有人可以测试我的代码并得到解决吗?