Ale*_*lex 6 html python iframe selenium selenium-webdriver
我正在尝试选择一个位于 iframe 中的元素,并且可能位于其他 iframe 中。
是否有可能在(python)selenium 中的某个(子)iframe 中选择一个元素而不选择之前的 iframe?有没有办法以某种方式“循环”每个 iframe 并检查在哪里可以找到我的元素......?
以及如何在 case 元素和 html 内容以及 iframe 可能只是加载...的情况下做到这一点?
无,所以不可能与任何交互WebElement的内iframe通过Selenium不切换到各自的iframe。
加载页面时,Selenium默认情况下 的焦点保持在 上Top Window。在Top Window包含了其他iframes和framesets。因此,当我们需要与WebElementiframe 内的a 进行交互时,我们必须iframe通过以下方法之一切换到相应的:
我们可以通过 3 种方式切换到帧。
按框架名称:
Name iframe 的属性,我们可以通过它切换到它。
例子:
driver.switch_to.frame("iframe_name")
Run Code Online (Sandbox Code Playgroud)
按帧 ID:
ID iframe 的属性,我们可以通过它切换到它。
例子:
driver.switch_to.frame("iframe_id")
Run Code Online (Sandbox Code Playgroud)
按帧索引:
假设页面有10个frame,我们可以通过index切换到iframe。
例子:
driver.switch_to.frame(0)
driver.switch_to.frame(1)
Run Code Online (Sandbox Code Playgroud)
切换回主框架:
我们可以使用default_content()或切换回主框架parent_frame()
例子:
driver.switch_to.default_content()
driver.switch_to.parent_frame()
Run Code Online (Sandbox Code Playgroud)
切换帧的更好方法是通过如下设置来诱导WebDriverWait预期帧的可用性:expected_conditionsframe_to_be_available_and_switch_to_it
通过Frame ID:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"id_of_iframe"))
Run Code Online (Sandbox Code Playgroud)通过Frame Name:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"name_of_iframe"))
Run Code Online (Sandbox Code Playgroud)通过Frame Xpath:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"xpath_of_iframe"))
Run Code Online (Sandbox Code Playgroud)通过Frame CSS:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.CSS_SELECTOR,"css_of_iframe"))
Run Code Online (Sandbox Code Playgroud)您可以在以下位置找到相关的详细讨论:
| 归档时间: |
|
| 查看次数: |
5285 次 |
| 最近记录: |