使用 JavaScript、操作類或 Web 驅動程式在元素上單擊?


我們可以使用 javascript、操作類和 Web 驅動程式方法點選一個元素。Selenium 可以藉助 execute_script() 方法執行 Javascript 中的命令。

我們必須在我們的程式碼中匯入 org.openqa.selenium.JavascriptExecutor 以使用 javascript 執行器。為了點選一個元素,我們首先必須使用滑鼠移動來移動到該元素。我們可以藉助 Selenium 中的 操作 類執行滑鼠移動。

我們必須使用 moveToElement() 方法。此方法將執行滑鼠移動,直到元素中間,然後執行點選,後跟構建() 和執行() 方法。我們必須在我們的程式碼中匯入 org.openqa.selenium.interactions.Actions 作為操作類。為了使用 Web 驅動程式方法點選一個元素,我們可以使用 click() 方法。

示例

使用 click() 方法的程式碼實現。

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;
public class ElementClk{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://tutorialspoint.tw/about/about_careers.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify element and click()
      WebElement l=driver.findElement(By.linkText("Terms of Use"));
      l.click();
      System.out.println("Current page title:" + driver.getTitle());
      driver.quit();
   }
}

示例

使用 Javascript 執行器的程式碼實現。

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 ElementClkJs{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://tutorialspoint.tw/about/about_careers.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify element
      WebElement l=driver.findElement(By.linkText("Terms of Use"));
      // click with Javascript Executor
      JavascriptExecutor j = (JavascriptExecutor) driver;
      j.executeScript("arguments[0].click();", l);
      System.out.println("Current page title:" + driver.getTitle());
      driver.quit();
   }
}

示例

使用操作類的程式碼實現。

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.interactions.Action;
import org.openqa.selenium.interactions.Actions
public class ElementClkActs{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://tutorialspoint.tw/about/about_careers.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify element
      WebElement l=driver.findElement(By.linkText("Terms of Use"));
      // Actions class with moveToElement() and click()
      Actions a = new Actions(driver);
      a.moveToElement(l).click();
      a.build().perform();
      System.out.println("Current page title:" + driver.getTitle());
      driver.quit();
   }
}

輸出

更新於: 2020-08-28

658 次檢視

開啟你的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.