如何在 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.com/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