如何在 Python Selenium WebDriver 中点击图片?
seleniumautomation testingtesting tools
我们可以使用 Python Selenium WebDriver 中的 click 方法点击图片。首先,我们需要借助 id、class、name、css、xpath 等定位器来识别图片。HTML 代码中的图片由 img 标签名表示。
让我们看看图片元素的 HTML 代码。

示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class ImageClk{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",
"C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//隐式等待
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//URL 启动
driver.get("https://www.tutorialspoint.com/index.htm");
// 识别图像
WebElement l = driver.findElement(By.xpath("//img[@alt='Tutorialspoint']"));
String s = l.getAttribute("title");
System.out.println("Title attribute value is :" + s);
l.click();
driver.quit();
}
}
输出


