Selenium 简明教程
Selenium with JavaScript Tutorial
Selenium Webdriver 可用于执行 JavaScript 命令,以便与网页上浏览器中显示的元素的 html 进行交互。这通过 JavaScriptExecutor 接口存档。JavaScript 命令与 Selenium Webdriver 一起使用的一个最常见的示例是在网页上执行滚动操作时。
Selenium Webdriver can be used to execute JavaScript commands to interact with the html of the elements appearing within a browser on a web page. This is archived using the JavaScriptExecutor interface. One of the most common examples where JavaScript commands are used along with Selenium Webdriver is while performing a scrolling operation on a web page..
Methods to Run JavaScript Commands
Selenium JavaScriptExecutor interface 中有各种方法可用于运行 JavaScript 命令。
There are various methods in the Selenium JavaScriptExecutor interface which can be used to run the JavaScript Commands.
Syntax
import org.openqa.selenium.JavascriptExecutor;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(script, args);
Example to Execute JavaScript Commands in Selenium
让我们以以下页面为例,我们将在其中使用 URL: Selenium Automation Practice Form 启动应用程序。然后,我们将获得文本:学生注册表,域名为 www.tutorialspoint.com 。接下来,我们将在 Name: 标签旁边的输入框中输入文本 JavaScript 。
Let us take an example of the below page, where we would launch an application with a URL: Selenium Automation Practice Form . Then we would obtain the text: Student Registration Form, and domain as www.tutorialspoint.com. Next we would enter the text JavaScript in the input box beside the Name: label.
然后,我们将点击 Login 按钮,之后我们将导航到另一个有文本的页面: Welcome, Login In 。最后,我们将刷新浏览器并再次获取文本、URL 和域为 Welcome, Login In, 、 Selenium Automation Practice Form 和 www.tutorialspoint.com 。
Then, we would click on the Login button, after which we would be navigated to another page having a text as Welcome, Login In. Finally, we would refresh the browser and again obtain the text, URL and domain as Welcome, Login In,, Selenium Automation Practice Form, and www.tutorialspoint.com.
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;
public class JavaScriptSelenium {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 25 secs
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
// Creating a reference variable JavaScriptExecutor interface.
JavascriptExecutor j = (JavascriptExecutor) driver;
// launching a URL
j.executeScript("window.location ='https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php'");
// getting current URL
String url = j.executeScript("return document.URL;").toString();
System.out.println("Getting the current URL: " + url);
//identify text
WebElement t = driver.findElement(By.xpath("//*[@id='practiceForm']/h1"));
String text = (String) j.executeScript("return arguments[0].innerText", t);
System.out.println("Text is: " + text);
// getting current domain
String domain = j.executeScript("return document.domain;").toString();
System.out.println("Getting the current domain: " + domain);
// enter text in input box
WebElement e = driver.findElement(By.xpath("//*[@id='name']"));
j.executeScript("arguments[0].value='JavaScript';", e);
// get text entered
String text1 = (String) j.executeScript("return arguments[0].value", e);
System.out.println("Entered text is: " + text1);
// perform click
WebElement b = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a"));
j.executeScript("arguments[0].click();", b);
//identify text
WebElement w = driver.findElement(By.xpath("//*[@id='signInForm']/h1"));
// get text after click
String text2 = (String) j.executeScript("return arguments[0].innerText", w);
System.out.println("Text found after clicking: " + text2);
// refresh browser
j.executeScript("history.go(0)");
// getting current URL after browser refresh
String url1 = j.executeScript("return document.URL;").toString();
System.out.println("Getting the current URL after browser refresh: " + url1);
//identify text again after refresh
WebElement y = driver.findElement(By.xpath("//*[@id='signInForm']/h1"));
// get text after refresh
String text3 = (String) j.executeScript("return arguments[0].innerText", y);
System.out.println("Text found after refresh: " + text3);
// getting current domain after browser refresh
String domain1 = j.executeScript("return document.domain;").toString();
System.out.println("Getting the current domain after browser refresh: " + domain1);
// Quitting browser
driver.quit();
}
}
Getting the current URL:
https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php
Text is: Student Registration Form
Getting the current domain: www.tutorialspoint.com
Entered text is: JavaScript
Text found after clicking: Welcome, Login In
Getting the current URL after browser refresh:
https://www.tutorialspoint.com/selenium/practice/login.php
Text found after refresh: Welcome, Login In
Getting the current domain after browser refresh:
www.tutorialspoint.com
Process finished with exit code 0
在以上示例中,我们启动了一个 URL,并在控制台中获取了带有消息的当前 URL: Getting the current URL: Selenium Automation Practice Form 。然后,使用控制台中的消息检索文本和域,分别是: Text is: Student Registration Form * and *Getting the current domain: www.tutorialspoint.com 。
In the above example, we had launched a URL and obtained the current URL with the message in the console - Getting the current URL: Selenium Automation Practice Form. Then retrieved the text and domain with the message in the console - Text is: Student Registration Form * and *Getting the current domain: www.tutorialspoint.com respectively.
接下来,我们在输入框中输入文本 Selenium,并使用控制台中带有消息的消息检索其值: Entered text is: Selenium 。然后,我们单击登录链接并通过控制台中带有消息的消息获得导航后的文本: Text found after clicking: Check Box 。
Next, we entered the text Selenium in an input box and retrieved its value with the message in the console - Entered text is: Selenium. Then, we clicked the Login link and got the text after navigation with the message in console: Text found after clicking: Check Box.
最后,我们刷新了页面并获得带有控制台中带有消息的页面当前 URL、文本和域,分别为: Getting the current URL after browser refresh: Selenium Automation Practice Form 、 Text found after refresh: Check Box 和 Getting the current domain after browser refresh: www.tutorialspoint.com 。
Finally, we had refreshed the page and obtained current URL, text and domain of the page with the messages in the console - Getting the current URL after browser refresh: Selenium Automation Practice Form, Text found after refresh: Check Box, and Getting the current domain after browser refresh: www.tutorialspoint.com respectively.
最后,接收到消息 Process finished with exit code 0 ,表示代码已成功执行。
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code
在本教程中,我们讨论了如何使用 Selenium Webdriver 使用 JavaScript。
In this tutorial, we had discussed how to use JavaScript using Selenium Webdriver.