首页 > 编程问答 >Selenium WebDriverWait 无法检测用于使用 Selenium 提交嵌入式视频答案的表单元素

Selenium WebDriverWait 无法检测用于使用 Selenium 提交嵌入式视频答案的表单元素

时间:2024-07-26 12:31:51浏览次数:8  
标签:python selenium-webdriver selenium-chromedriver

我一直在使用 Selenium 编写一个机器人来自动化学校强迫我做的系列教程。 在本教程中,有一个视频在中途提示用户填写表格和一些问题。 我一直在尝试使用此代码在视频播放时等待,直到表单可见

    WebDriverWait(driver, 1000).until(
        EC.presence_of_element_located((By.ID, "answer_content_A11A59DC-2C74-11EE-83FC-C435C68A4852"))
    )


此 ID 在视频开始时不可见,但一旦出现表单就可以找到。 但是,该脚本永远不会超过 WebDriverWait() 块。

我也尝试过根据其他元素的出现情况进行等待,但它们似乎提出了同样的问题。 每次他们卡在 WebDriverWait() 上时。

然后我尝试等待 30 秒让表单变得可见,但这也不起作用。

    #Presses Play on Video
    driver.find_element(By.CLASS_NAME, "slip_button_icon").click()
    #Full duration of Video
    time.sleep(30)
    driver.find_element(By.ID, "answer_content_A11A59DC-2C74-11EE-83FC-C435C68A4852").click()

即使当我命令 F ID 时也会出现此错误在我的 Selenium 浏览器中,我能够找到它。

发生错误:消息:没有这样的元素:无法定位元素:{“method”:“css选择器”,“selector”:“[id =“answer_content_A11A59DC- 2C74-11EE-83FC-C435C68A4852"]"} (会话信息:chrome=127.0.6533.72);有关此错误的文档,请访问: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception


The problem is likely that the element you are trying to find is inside an iframe. By default, Selenium looks for elements in the top-level document. You need to switch the driver's context to the iframe before searching for elements within it.

Here's a breakdown of how to solve this and improve your code:

1. Identify the iframe:

  • Inspect the page source: Use your browser's developer tools (right-click on the page -> "Inspect" or "Inspect Element") to find the iframe containing the form. Look for an <iframe> tag.
  • Get the iframe's name or ID: The iframe will usually have a name or id attribute. You will need this to switch to the iframe.

2. Switch to the iframe:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# ... your code ...

# Example: Assuming the iframe has an id "videoPlayerFrame"
iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "videoPlayerFrame")))
driver.switch_to.frame(iframe) 

3. Find and interact with the element:

# Now that you're in the iframe's context, try finding the element again
form_element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "answer_content_A11A59DC-2C74-11EE-83FC-C435C68A4852"))
)

# Interact with the element as needed
form_element.click() 

4. Switch back to the default content (important):

  • Once you are finished interacting with elements inside the iframe, switch back to the main content of the webpage:
driver.switch_to.default_content()

Additional Tips:

  • Explicit Waits are key: Use WebDriverWait to wait for elements to become interactable. This is much more reliable than fixed time.sleep() calls.
  • Handle potential errors: Use try-except blocks to handle situations where the iframe or element might not be found.
  • Dynamic IDs: If the element ID is dynamically generated, explore other locators like CSS selectors or XPath to target it reliably.

Remember: Replace the example iframe ID and element ID with the actual values from your webpage.

标签:python,selenium-webdriver,selenium-chromedriver
From: 78795633

相关文章

  • Python 教程(三):字符串特性大全
    目录专栏列表前言1.字符串基础2.字符串方法字符串查询字符串修改字符串切片3.字符串格式化旧式格式化(`%`操作符)`str.format()`方法f-string(Python3.6+)4.字符串编码5.Unicode和ASCII6.正则表达式7.字符串比较8.字符串连接9.字符串不可变性10.字符串的内......
  • python+flask计算机毕业设计新冠肺炎疫情人员统计及打卡系统(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景自新冠肺炎疫情爆发以来,全球公共卫生体系面临前所未有的挑战。疫情防控工作的高效开展,依赖于对人员流动、健康状况及疫情数据的精准掌握与......
  • python+flask计算机毕业设计基于智能匹配的体育场馆预约系统App(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着全民健身意识的日益增强,体育场馆作为民众参与体育活动的重要场所,其利用率与便捷性成为了社会关注的焦点。然而,传统的体育场馆预约方式......
  • Vonage 语音 API - 使用 python 出现错误
    我正在尝试使用vonage语音api模拟语音通话。我正在尝试使用python来做到这一点。我创建了一个.env文件并更新了应用程序id和私钥值的值,而不是路径(不确定从哪里获取它)。这是下面编写的代码:#!/usr/bin/envpython3importosfromos.pathimportjoin,dirname......
  • 数据清洗与预处理:使用 Python Pandas 库
    数据清洗与预处理:使用PythonPandas库1.简介数据清洗与预处理是数据科学和机器学习中必不可少的步骤。它涉及识别和处理原始数据中的错误、不一致和缺失值,以确保数据的质量和可靠性。Python的Pandas库提供了强大的工具,简化了数据清洗和预处理的过程。2.数据加载与探索......
  • 【Python】成功解决:`FileExistsError: [Errno 17] File exists: ‘xxx’`
    【Python】成功解决:FileExistsError:[Errno17]Fileexists:‘xxx’在Python编程中,处理文件和目录是常见的任务之一。然而,当我们尝试执行某些文件操作,如创建新文件或目录时,如果目标文件或目录已经存在,就可能会遇到FileExistsError异常。这个错误通常伴随着消息[Errno1......
  • (三)Python基本数据类型
    Python的基本数据类型包括整数类型、浮点数类型和复数类型。下面分别介绍这些数据类型以及数值运算操作符和数值运算函数。整数类型(int):整数类型表示没有小数部分的数字,可以是正数、负数或零。例如:a=5b=-3c=02.浮点数类型(float):浮点数类型表示有小数部分的数字,可以......
  • 【Python自动化办公】用Pandas库自动化操作Excel表格,从读取、写入到数据处理和分析
    文末免费赠送精品编程资料~~前言Python的第三方Pandas库是数据处理和分析中的利器,其强大的功能可以帮助我们轻松地对Excel表格进行自动化操作。接下来,我们将介绍九个用Pandas库操作Excel的编程例子,并且每个例子都会涉及不同的知识点,确保全面掌握这个主题。1.读取和写入E......
  • 总结24个Python接单赚钱平台与详细教程,兼职月入5000+
    如果说当下什么编程语言最靠谱或者比较适合搞副业?答案肯定100%是:Python。python是所有语法中最简单易上手的语言,不需要特别的的英语词汇量,逻辑思维也不需要很差就能上手。而且学会了之后就能编写代码爬取各种数据,制作各种图表,提升工作效率。而且还能利用业余时间接点私活......
  • python安装第三方库的国内镜像
    直接:pipconfigsetglobal.index-urlhttps://pypi.doubanio.com/simple设置了全局的第三方库的下载文件镜像请求网址。安装第三方库:pipinstallscrapy--scrapy第三方库名称 pip从国内镜像安装的命令使用中国大陆地区的Python包镜像服务时,可以通过修改p......