Selenium and iframe in html
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
assuming that driver is a healthy instance of webdriver. To continue with the default content do driver.switch_to.default_content()
EDIT: When you have switched to needed frame, locate your webelement as you always do. I guess (but not sure) in your case this will be a html/body, so
el = driver.find_element_by_xpath('html/body')
should be fine. And perform
el.send_keys('keys_to_send')
EDIT2: Before sending keys you may have to focus on the element (click should do the thing, a child element should appear). Or you can just place the needed text via JS.
driver.execute_script('document.body.innerHTML = "%s"' % text_var)
How to enter text in an iframe with Selenium and Python
There is an iframe
<iframe frameborder="0" hspace="0" scrolling="no" src="/GBS/Authentication/Authenticate?redirectUrl=%2FGBS%2FProject&requestMode=Setup&isIframe=True" allowtransparency="true" style="border: none; width: 345px; height: 513px;"></iframe>
You can do the following to swap to it.
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@id='oxygen-container']/iframe")))
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Here's the full thing
driver.get("https://gbs.autodesk.com/GBS/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Sign In"))).click()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@id='oxygen-container']/iframe")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "userName"))).send_keys("test@mail.com")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "verify_user_btn"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "password"))).send_keys("mypassword")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "btnSubmit"))).click()
How to send text with selenium python to a iframe within a !DOCTYPE html?
The <p>
element is within an <iframe>
so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using
CSS_SELECTOR
:driver.get('https://www.t-online.de/themen/e-mail')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#textarea-WYSIWYG_ifr")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "body.mce-content-body#tinymce > p"))).send_keys("anynaynanya")Using
XPATH
:driver.get('https://www.t-online.de/themen/e-mail')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='textarea-WYSIWYG_ifr']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//body[@id='tinymce' and @class='mce-content-body']/p"))).send_keys("anynaynanya")Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Reference
You can find a couple of relevant discussions in:
- Ways to deal with #document under iframe
- Switch to an iframe through Selenium and python
HTML page seems to add iframe element when opening chrome by selenium in python
You are facing issue due to synchronization .Kinddly find below solution to resolve your issue:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r"path ofchromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get("https://www.ncbi.nlm.nih.gov/pubmed")
iframe = wait.until(EC.presence_of_element_located((By.TAG_NAME, "iframe")))
driver.switch_to.frame(iframe)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'Sign in to NCBI')]"))).click()
Output:
Inspect element:
How to click on a link inside an iframe which is inside a frame using Selenium Webdriver and Java
I was able to get get it working by switching content by index. Using System.out.println(driver.getPageSource());
to log page source to console and make sure i am working at the right spot.
I tried to find iframes by tag and print their name as following.
List<WebElement> elements = DriverContext.driver.findElements(By.tagName("iframe"));
elements.forEach(element -> System.out.println(element.getAttribute("name"))); // printed 'menu' and 'body', perfect
But, while switching content to iframe using name, tried also using WebDriverWait,
driver.switchTo().frame(driver.findElement(By.name("menu")));
for whatever reason, it didn't work and kept throwing the exception as in the question.
Using index to switch to iframe followed by click on the element worked.
driver.switchTo().frame(0); // switch to frame
driver.switchTo().frame(0); // switch to first iframe
driver.findElement(By.id("elmt_XXXX_subMenu")).click();
Also, i had used WebDriverWait to wait for page load.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(webDriver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete"));
Get fully generated DOM elements inside iframe using selenium and phantomJS with python
You can't get a full page_source. In the case of iframe, you should use the following command: switch_to.frame(iframe_element), so you can get an element inside
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
self.driver = webdriver.PhantomJS()
self.driver.get(url)
WebDriverWait(self._driver, 50).until(
EC.presence_of_all_elements_located
((By.XPATH,
'//iframe[@id="iframegame"]'))
)
iframe_element = self.driver.find_element_by_xpath('//iframe[@id="iframegame"]')
self.driver.switch_to.frame(iframe_element)
tag = self.driver.find_element_by_xpath('//tag')
And back again, you can get an outer element of iframe using the following command;
self.driver.switch_to.default_content()
Related Topics
Remove Non-Numeric Rows in One Column with Pandas
Python Scope: "Unboundlocalerror: Local Variable 'C' Referenced Before Assignment"
Does Spark Predicate Pushdown Work with Jdbc
Error Installing Geopandas:" a Gdal API Version Must Be Specified " in Anaconda
Reference List Item by Index Within Django Template
Fill Username and Password Using Selenium in Python
How to Read File N Lines at a Time
How to Display a Pandas Data Frame with Pyqt5/Pyside2
Should I Call Close() After Urllib.Urlopen()
Access Item in a List of Lists
How to Create an Encrypted Zip File
How to Specify Your Own Distance Function Using Scikit-Learn K-Means Clustering