【问题标题】:ReCaptcha download audio file?ReCaptcha 下载音频文件?
【发布时间】:2021-01-20 16:42:58
【问题描述】:

请让我知道我做错了什么

使用来自:How to interact with the reCAPTCHA audio element using Selenium and Python的代码

我想下一步获取音频的 src 文件以下载它,所以我在提供的代码结束后准确地写了以下内容:

# get the mp3 audio file
src = driver.find_element_by_id("audio-source").get_attribute("src")

但是 Python 返回运行时错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="audio-source"]"}
  (Session info: chrome=88.0.4324.96)

注意:我照原样复制了该代码,只添加了最后一行

为了让您信服,这里是我的完整代码:

def tmp():
    from selenium.webdriver.common.keys import Keys

    # recaptcha libraries
    import speech_recognition as sr
    import urllib
    # import pydub

    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

    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='/Users/ahmad/Desktop/chromedriver')
    driver.get("https://www.google.com/recaptcha/api2/demo")
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(
        (By.CSS_SELECTOR, "iframe[src^='https://www.google.com/recaptcha/api2/anchor']")))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
    driver.switch_to.default_content()
    WebDriverWait(driver, 10).until(
        EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='recaptcha challenge']")))
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-audio-button"))).click()

    # get the mp3 audio file
    src = driver.find_element_by_id("audio-source").get_attribute("src")


我在附近的元素上尝试了以下以获得它的 href 值并且它有效,我唯一的问题是上面的内容:

src=WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "a.rc-audiochallenge-tdownload-link"))).text

src=src.get_attribute('href')

print(src)

它处理的元素是:


我也试过这个:

src=WebDriverWait(driver, 10).until(
    EC.invisibility_of_element((By.XPATH, "//*[@id=\"audio-source\"]")))

src=src.get_attribute('src')

但我得到一个错误:

src=src.get_attribute('src')

AttributeError: 'bool' 对象没有属性 'get_attribute'

【问题讨论】:

  • 所示代码不能是完整代码。它至少有错误的缩进并且错过了导入。
  • @MisterMiyagi 修复了它,但这并没有任何影响,尤其是相同的代码在那里发布了答案
  • 重点不是给你修。重点是为我们提供minimal reproducible example
  • 已经提供了一个例子,谷歌页面也是公开的,我已经添加了它的 html 代码的图像,我已经在那个框架内但不知道是什么问题
  • 我不知道你是怎么通过这条线的iframe[src^='https://www.google.com/recaptcha/api2/anchor,因为https://www.google.com/recaptcha/api2/demo没有这样的iframe

标签: python html python-3.x selenium


【解决方案1】:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
    (By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
driver.switch_to.default_content()
WebDriverWait(driver, 10).until(
    EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='recaptcha challenge']")))
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-audio-button"))).click()
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, ".rc-audiochallenge-play-button button")))

# get the mp3 audio file

src = driver.find_element_by_id("audio-source").get_attribute("src")
print(src)

只需为 .rc-audiochallenge-play-button 按钮再添加一个 wiat

下载你应该使用:

import urllib.request
urllib.request.urlretrieve(src, "src.mp3")

【讨论】:

  • 为什么在等待你有点击?
  • 我删除了它你不需要它,即使它在那里也可以
  • 另外,请注意 'audio-source' 是不同的,并且不包含在 audiochallenge-play-button 中
  • 我没有检索到您应该等待音频播放提示出现,然后才能检索 src 属性,src 属性仍然从同一个源中检索
猜你喜欢
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多