如何在 Selenium webdriver Python 中點選影像?


使用 click 方法可以在 Python 中使用 Selenium webdriver 點選影像。首先,我們必須藉助 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();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://tutorialspoint.tw/index.htm");
      // identify image
      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();
   }
}

輸出

更新於: 2021-04-03

3K+ 次檢視

開啟你的 職業生涯

完成課程獲得認證

開始
廣告