简体   繁体   English

Python selenium - WebDriverException:目标框架分离 - 用于登录按钮

[英]Python selenium - WebDriverException: target frame detached - for Login button

I am trying to automate login to the website https://research.axiscapital.co.in/ .我正在尝试自动登录网站https://research.axiscapital.co.in/ I am able to add username and password.我可以添加用户名和密码。 I have also automated solving the captcha.我还自动解决了验证码。 But after it solves the captcha, I am unable to click the login button.但是解决验证码后,我无法单击登录按钮。 I get the WebDriverException: target frame detached exception.我得到WebDriverException: 目标框架分离异常。 I am adding the code below (without the real username and password) for assistance.我正在添加下面的代码(没有真实的用户名和密码)以寻求帮助。

NOTE: As soon as the captcha verification expires, the login button becomes clickable again.注意:验证码验证过期后,登录按钮将再次变为可单击。 Kindly help me with it.请帮助我。

import requests
import time
import os
# Added for Selenium
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

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.common.keys import Keys

# TO MAKE THE SCRAPING FASTER
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver = webdriver.Chrome()
driver.maximize_window()


driver.get("https://research.axiscapital.co.in/")

filename = '1.mp3'
delayTime = 2
googleIBMLink = 'https://speech-to-text-demo.ng.bluemix.net/'
audioToTextDelay = 10


def audioToText(mp3Path):
    print("1")
    driver.execute_script('''window.open("","_blank");''')
    driver.switch_to.window(driver.window_handles[1])
    print("2")
    driver.get(googleIBMLink)
    delayTime = 10
    # Upload file
    time.sleep(1)
    print("3")
    # Upload file
    time.sleep(1)
    root = driver.find_element_by_id('root').find_elements_by_class_name('dropzone _container _container_large')
    btn = driver.find_element(By.XPATH, '//*[@id="root"]/div/input')
    btn.send_keys('D:\\blogs\\1.mp3')
    # Audio to text is processing
    time.sleep(delayTime)
    #btn.send_keys(path)
    print("4")
    # Audio to text is processing
    time.sleep(audioToTextDelay)
    print("5")
    text = driver.find_element(By.XPATH, '//*[@id="root"]/div/div[7]/div/div/div').find_elements_by_tag_name('span')
    print("5.1")
    result = " ".join( [ each.text for each in text ] )
    print("6")
    driver.close()
    driver.switch_to.window(driver.window_handles[0])
    print("7")
    return result
def saveFile(content,filename):
    with open(filename, "wb") as handle:
        for data in content.iter_content():
            handle.write(data)

wait = WebDriverWait(driver,60)

wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@id="Username"]'))).send_keys(username)
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@name="Password"]'))).send_keys(password)

time.sleep(1)
googleClass = driver.find_elements_by_class_name('g-recaptcha')[0]
time.sleep(2)
outeriframe = googleClass.find_element_by_tag_name('iframe')
time.sleep(1)
outeriframe.click()
time.sleep(2)

allIframesLen = driver.find_elements_by_tag_name('iframe')
time.sleep(1)
audioBtnFound = False
audioBtnIndex = -1

for index in range(len(allIframesLen)):
    driver.switch_to.default_content()
    iframe = driver.find_elements_by_tag_name('iframe')[index]
    driver.switch_to.frame(iframe)
    driver.implicitly_wait(delayTime)
    try:
        audioBtn = driver.find_element_by_id('recaptcha-audio-button') or driver.find_element_by_id('recaptcha-anchor')
        audioBtn.click()
        audioBtnFound = True
        audioBtnIndex = index
        break
    except Exception as e:
        pass

if audioBtnFound:
    try:
        while True:
            href = driver.find_element_by_id('audio-source').get_attribute('src')
            response = requests.get(href, stream=True)
            saveFile(response,filename)
            response = audioToText(os.getcwd() + '/' + filename)
            print(response)
            driver.switch_to.default_content()
            iframe = driver.find_elements_by_tag_name('iframe')[audioBtnIndex]
            driver.switch_to.frame(iframe)
            inputbtn = driver.find_element_by_id('audio-response')
            inputbtn.send_keys(response)
            inputbtn.send_keys(Keys.ENTER)
            time.sleep(2)
            
            errorMsg = driver.find_elements_by_class_name('rc-audiochallenge-error-message')[0]
            if errorMsg.text == "" or errorMsg.value_of_css_property('display') == 'none':
                print("Success")
                break
    except Exception as e:
        print(e)
        print('Caught. Need to change proxy now')
else:
    print('Button not found. This should not happen.')


time.sleep(4)

wait.until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Login"]'))).click()

You forgot to switch to default context after successfully completing the captcha.成功完成验证码后,您忘记切换到默认上下文。

put driver.switch_to.default_content() before break .driver.switch_to.default_content()放在break之前。

Edit: the success block would look like this.编辑:成功块看起来像这样。

                print("Success")
                driver.switch_to.default_content()
                break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Selenium - 如何修复 WebDriverException:消息:目标框架已分离 - Python Selenium - How to fix WebDriverException: Message: target frame detached 代码抛出在 selenium/python 中分离的随机目标帧 - Code throws random target frame detached in selenium/python selenium.common.exceptions.WebDriverException:消息:已断开连接:通过Python通过ChromeDriver和Selenium收到了Inspector.detached事件 - selenium.common.exceptions.WebDriverException: Message: disconnected: received Inspector.detached event with ChromeDriver and Selenium through Python Python Selenium错误:“WebDriverException:'login'可执行文件需要在PATH中。” - Python Selenium error: “WebDriverException: 'login' executable needs to be in PATH.” Selenium 中的 WebDriverException 使用 python 3 - WebDriverException in Selenium using python 3 如何解决“WebDriverException: Message: TypeError: can't access dead object”并使用 Selenium 和 Python 等待帧可用 - How to address “WebDriverException: Message: TypeError: can't access dead object” and wait for a frame to be available using Selenium and Python Python - 硒:selenium.common.exceptions.WebDriverException - Python - Selenium: selenium.common.exceptions.WebDriverException chrome更新后,消息:目标框架分离 - After chrome update, Message: target frame detached Python / Selenium:WebDriverException:消息:u为空' - Python / Selenium: WebDriverException: Message: u'a is null' Python的Selenium驱动程序在Python中引发WebDriverException - WebDriverException thrown in Python for Selenium driver for Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM