• Selenium Video Tutorials

Selenium 和 JavaScript 教程



Selenium Webdriver 可用于执行 JavaScript 命令来与网页浏览器中出现的元素的 html 交互。这是使用 JavaScriptExecutor 接口实现的。JavaScript 命令与 Selenium Webdriver 一起使用的最常见示例之一是在网页上执行滚动操作时。

运行 JavaScript 命令的方法

Selenium JavaScriptExecutor 接口中,有多种方法可用于运行 JavaScript 命令。

语法

import org.openqa.selenium.JavascriptExecutor;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(script, args);

Selenium 中执行 JavaScript 命令的示例

让我们以以下页面为例,我们将使用 URL 启动应用程序:Selenium 自动化实践表单。然后我们将获取文本:学生注册表单,以及域名 www.tutorialspoint.com。接下来,我们将在“姓名:”标签旁边的输入框中输入文本JavaScript

Selenium Javascript Tutorial 1

然后,我们将单击“登录”按钮,之后我们将导航到另一个页面,该页面显示文本欢迎,登录。最后,我们将刷新浏览器并再次获取文本、URL 和域名,分别为欢迎,登录Selenium 自动化实践表单www.tutorialspoint.com

Selenium Javascript Tutorial 2

代码实现

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://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://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://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,并在控制台中显示消息 - 获取当前 URL:Selenium 自动化实践表单。然后分别检索了文本和域名,并在控制台中显示消息 - 文本为:学生注册表单获取当前域名:www.tutorialspoint.com

接下来,我们在输入框中输入了文本 Selenium 并检索了其值,并在控制台中显示消息 - 输入的文本为:Selenium。然后,我们单击登录链接并在导航后获取文本,并在控制台中显示消息:单击后找到的文本:复选框

最后,我们刷新了页面并获取了页面的当前 URL、文本和域名,并在控制台中显示消息 - 浏览器刷新后获取当前 URL:Selenium 自动化实践表单刷新后找到的文本:复选框浏览器刷新后获取当前域名:www.tutorialspoint.com

最后,收到消息进程已完成,退出代码为 0,表示代码已成功执行。

在本教程中,我们讨论了如何使用 Selenium Webdriver 使用 JavaScript。

广告