|
| 1 | +# Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The SFC licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# Unless required by applicable law or agreed to in writing, |
| 11 | +# software distributed under the License is distributed on an |
| 12 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 13 | +# KIND, either express or implied. See the License for the |
| 14 | +# specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
1 | 17 | from selenium import webdriver
|
| 18 | +from selenium.webdriver.common.by import By |
| 19 | + |
| 20 | +#set chrome and launch web page |
| 21 | +driver = webdriver.Chrome() |
| 22 | +driver.get("https://www.selenium.dev/selenium/web/iframes.html") |
| 23 | + |
| 24 | +# --- Switch to iframe using WebElement --- |
| 25 | +iframe = driver.find_element(By.ID, "iframe1") |
| 26 | +driver.switch_to.frame(iframe) |
| 27 | +assert "We Leave From Here" in driver.page_source |
| 28 | + |
| 29 | +email_element = driver.find_element(By.ID, "email") |
| 30 | +email_element. send_keys( "[email protected]") |
| 31 | +email_element.clear() |
| 32 | +driver.switch_to.default_content() |
| 33 | + |
| 34 | +# --- Switch to iframe using name or ID --- |
| 35 | +iframe1=driver.find_element(By.NAME, "iframe1-name") # (This line doesn't switch, just locates) |
| 36 | +driver.switch_to.frame(iframe) |
| 37 | +assert "We Leave From Here" in driver.page_source |
| 38 | + |
| 39 | +email = driver.find_element(By.ID, "email") |
| 40 | +email. send_keys( "[email protected]") |
| 41 | +email.clear() |
| 42 | +driver.switch_to.default_content() |
| 43 | + |
| 44 | +# --- Switch to iframe using index --- |
| 45 | +driver.switch_to.frame(0) |
| 46 | +assert "We Leave From Here" in driver.page_source |
| 47 | + |
| 48 | +# --- Final page content check --- |
| 49 | +driver.switch_to.default_content() |
| 50 | +assert "This page has iframes" in driver.page_source |
| 51 | + |
| 52 | +#quit the driver |
| 53 | +driver.quit() |
2 | 54 |
|
| 55 | +#demo code for conference |
0 commit comments