如何在 Selenium 中獲取影像的原圖?
我們可以在 Selenium 中獲取影像的原始碼。HTML 文件中的影像具有 <img> 標籤名。每個影像還具有一個 src 屬性,其中包含頁面中影像的原始碼。

若要在 Selenium 中獲取任何屬性,我們必須使用 getAttribute() 方法。該方法將屬性名作為引數。因此,若要獲取 src 屬性,我們必須編寫 getAttribute("src").
示例
程式碼實現。
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 Imagesrc{
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/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// identify image
WebElement l =driver.findElement(By.xpath("//img[@title='Tutorialspoint']"));
//getAttribute() to get src of image
System.out.println("Src attribute is: "+ l.getAttribute("src"));
driver.quit();
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP