Click on cross domain iframe element using Rselenium
You can just select the first iframe
and pass it to the switchToFrame
method:
webElem <- remDr$findElements("css", "iframe")
remDr$switchToFrame(webElem[[1]])
webel <- remDr$findElement(using = "xpath", "//input[@id = 'TB1']")
Clicking on a custom element with Selenium (RSelenium)
If an element you wish to extract the value is in an Iframe, it needs to be switched focus first and then you can proceed to take the element by ID/Class
for example, I used to click a Button inside an Iframe from a site called "https://forexsb.com/historical-forex-data"
iframe_detect <- remDr$findElements(using = "xpath", value = "//iframe[@id='data-app-frame']")
remDr$switchToFrame(iframe_detect[[1]])
load_data_element <- remDr$findElement(using="xpath", value="//button[@id='btn-load-data']")
load_data_element$clickElement()
In above what you use of defining Class element:
down <- remDr$findElement(using="xpath", value="//mat-icon[@class='mat-icon.notranslate.icon-arrow-down-tail.mat-icon-no-color']")
I guess this is a proper full class identifier, since there is multiple name of class joint with dot operator. But in my experience, if using these can come out didn't work, I use just the first class name before the dot operator
so if I had to rewrite (note: only work if there are no more element have the same class name, or else it may take the whole element which match by the Class):
down <- remDr$findElement(using="xpath", value="//mat-icon[@class='mat-icon']")
click on iframe element using Python selenium
You need to use switch_to_frame()
driver.switch_to_frame("result")
driver.switch_to_frame(driver.find_element_by_css_selector("body>iframe"))
driver.find_element_by_css_selector("input.ex2").click()
Once you're done in the iframe
, you can switch back to the top frame using:
driver.switch_to_default_content()
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"));
Python/Selenium Access element inside of iframe
You can use the below code to switch to frame and then interact with the desired element :-
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(@sandbox,'allow-popups-to-escape-sandbox')]")))
ele = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.analytics-ui-application")))
#ele is a web element so you can trigger .click, .text or any other web element methods on it.
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Python selenium How to click an element in iframe?
The element you are looking after is inside nested iframe
. You need to switch both the
iframes.
Use following css selector to identify the iframe
.
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='landingpage']")))
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='plus.web.de']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "save-all-conditionally"))).click()
Or Use below xpath
to identify the iframe.
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@name='landingpage']")))
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src,'plus.web.de')]")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "save-all-conditionally"))).click()
RSelenium findElement function
The data you want to scrape is in iframe
.
One way to do is,
remDr$navigate('https://csi2.valutrades.com/sentimentgraph.php')
webElem <- remDr$findElement('xpath', '//*[@id="EURUSD"]')
webElem$getElementText()
[[1]]
[1] "EURUSDLongShort44.3%55.7%"
Related Topics
Error When Mapping in Ggmap with API Key (403 Forbidden)
Map Array of Strings to an Array of Integers
Why Does Nls Function Not Work in Ggplot2
R Dplyr Subset with Missing Columns
Why Does 1..99,999 == "1".."99,999" in R, But 100,000 != "100,000"
How to Convert a Numeric Value into a Date Value
Terms of a Sum in a R Expression
Create All Subvectors of a Certain Length (Moving Window)
R: How to Get a Sum of Two Distributions
Error Using T.Test() in R - Not Enough 'Y' Observations
How to Color Bar Plots When Using ..Prop.. in Ggplot
Drop Columns That Take Less Than N Values
Manual Simulation of Markov Chain in R
Shiny Error in Match.Arg(Position):'Arg' Must Be Null or a Character Vector
How to Add New Calculated Variables to a Data Frame
Unexpected Date When Converting Posixct Date-Time to Date - Timezone Issue