我一直在使用 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
orid
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 fixedtime.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