เราสามารถคลิกลิงค์ด้วยการคลิกของไดรเวอร์เว็บและคลิกจาวาสคริปต์ สำหรับการคลิกลิงก์ Selenium webdriver เราสามารถใช้ข้อความลิงก์และตัวระบุข้อความลิงก์บางส่วน เราสามารถใช้วิธีการ driver.findElement(By.linkText()) และ driver.findElement(By.partialLinkText()) เพื่อคลิก
ลิงก์ในโค้ด html อยู่ในแท็กสมอ ข้อความลิงก์ที่อยู่ภายในแท็กสมอจะถูกส่งเป็นอาร์กิวเมนต์ไปยัง driver.findElement(By.linkText()) กระบวนการ. ข้อความลิงก์ที่ตรงกันบางส่วนที่อยู่ภายในแท็ก anchor จะถูกส่งผ่านเป็นอาร์กิวเมนต์ไปยัง driver.findElement(By.partialLinkText(
ให้เราดูโค้ด html ของลิงก์ที่มีแท็ก anchor

ตัวอย่าง
การติดตั้งโค้ด
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
public class DriverClick{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
// identify link with link text locator
driver.findElement(By.linkText("Write for us")).click();
System.out.println("Page title after click: " + driver.getTitle());
}
} เรายังดำเนินการเว็บได้ เช่น การคลิกลิงก์ด้วย Javascript Executor ใน Selenium เราจะใช้ executeScript เมธอดและส่ง อาร์กิวเมนต์ index.click() และ webelement ที่จะถูกคลิกเป็นอาร์กิวเมนต์ของวิธีการ
ตัวอย่าง
การติดตั้งโค้ดด้วย Javascript executor
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.By;
public class DriverClickJs{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
// identify link
WebElement l = driver.findElement(By.linkText("Write for us"));
//click link with Javascript Executor
JavascriptExecutor j = (JavascriptExecutor) driver;
j.executeScript("arguments[0].click();", l);
System.out.println("Page title after click: " + driver.getTitle());
}
} ผลลัพธ์
