Computer >> Máy Tính >  >> Lập trình >> HTML

Làm cách nào để đặt hiển thị kiểu của một phần tử html trong thử nghiệm selen?

Chúng ta có thể thiết lập hiển thị kiểu của một phần tử html bằng Selenium webdriver. DOM tương tác với các phần tử trên trang với sự trợ giúp của Javascript. Selenium thực thi các lệnh Javascript bằng cách sử dụng sự trợ giúp của executeScript phương pháp. Các lệnh được thực thi được chuyển dưới dạng đối số cho phương thức.

Một số thao tác như đặt hiển thị kiểu được thực hiện bởi Javascript Executor . getElementById có thể được sử dụng để xác định vị trí của phần tử. Sau đó, chúng tôi phải áp dụng style.display trên webelement và đặt kiểu hiển thị.

Cú pháp

executor.executeScript
("document.getElementById('gsc-i-id1').style.display='block';");

Ví dụ

Triển khai mã.

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;
import org.openqa.selenium.JavascriptExecutor;
public class ElementStyleSet{
   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/index.htm");
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // Javascript executor class with executeScript method
      JavascriptExecutor j = (JavascriptExecutor) driver;
      // set the display with style.display method
      j.executeScript ("document.getElementById('gsc-i-id1').style.display='block';");
      driver.close()
   }
}