
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you use Selenium to execute javascript within a frame?
We can use Javascript within a frame with Selenium. It can run Javascript commands by the executeScript method. The command to be executed is passed as an argument to the method.
Next, we have to return the values from the Javascript command with the return keyword. We have to take the help of the window.length command in Javascript to count the number of frames in a page.
Syntax
JavascriptExecutor j = (JavascriptExecutor) driver; int i = Integer.parseInt(j.executeScript("return window.length").toString());
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class FramesJS{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.uitestpractice.com/Students/Switchto"); driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS); // Javascript executor to return value frame count JavascriptExecutor j = (JavascriptExecutor) driver; int i = Integer.parseInt (j.executeScript("return window.length").toString()); System.out.print("Frame count is: " +i); // switching to the frame and get text within frame driver.switchTo().frame("iframe_a"); WebElement l = driver.findElement(By.cssSelector("h1")); System.out.print("Text within frame: " + l.getText()); driver.quit(); } }
Output
- Related Questions & Answers
- How to execute a Javascript using Selenium in Python?
- How to execute a Javascript function in Python with Selenium?
- How do you get selenium to recognize that a page loaded?
- How do I use Selenium IDE?
- How do you check scroll position using selenium?
- How do you convert a JavaScript date to UTC?
- Do you think declarations within Python class are equivalent to those within __init__ method?
- How do I use Selenium with Ruby?
- How do you get a timestamp in JavaScript?
- How do you use regular expressions in Cucumber?
- How do you automatically download a Pdf with Selenium Webdriver in Python?
- How do you focus on new windows with selenium ide?
- How do you test pages that require authentication with Selenium?
- How do you make Selenium 2.0 wait for the page to load?
- How do you convert a string to a character array in JavaScript?
Advertisements