Selenium-使用python从html获取音频src



我正试图使用selenium从repatcha音频源中获取特定属性。

然而,我不确定如何做到这一点https://www.google.com/recaptcha/api2/demo

  1. 点击";"我不是机器人";

  2. 选择耳机

  3. 提取src链接

    <audio id="audio-source" src="https://www.google.com:443/recaptcha/api2/payload?p=06AGdBq278w_OvG1dn_-_sgoVrqxLWcBq0IBkj2htJcsS-iTT3HtmwlhcTfBrcbQelxGI0hiep-082RypK_wZUTE-XzVbmcJ8zANM9l5O_0ka3x_7E_Hf_-vGqcRHCdRO7w2krqcgZDJSu1wj5wVyWhbDGITl55YsOs21NoX4aHk38173DPPu-Kj6T3mnqnA_3rMsdTkOUtMyl&amp;k=6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-" style="display: none"></audio>
    

我想检索src链接并打印出

我可以知道有没有什么方法可以让我使用硒吗?


到目前为止,我的代码允许我加载到重述演示页面中->单击"我不是机器人"->点击音频按钮

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH="C:Program Files (x86)chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("http://localhost/recaptcha-v2/")
# 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()

#This works, i can get the captcha token
# Src_URL = driver.find_element_by_id('recaptcha-token').get_attribute('value')
#This does not work, it can't locate the src
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "audio-source")))
Src_URL = driver.find_element_by_id('audio-source').get_attribute('src')
print(Src_URL)

请告知谢谢!

我使用以下代码从audio标签中提取src属性(如果audio标签在另一个iframe中,请更改iframe(-

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)
driver.get("YourURL")
# you can also use time.sleep(5)
wait.until(expected_conditions.presence_of_element_located((By.ID, "audio-source")))
Src_URL = driver.find_element_by_id('audio-source').get_attribute('src')
print(Src_URL)

最新更新