有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    尝试使用:

    String cssValue = driver.findElement(By.id("element_id")).getAttribute("style");
    

    不,这将从该元素的代码中提取所有css样式部分;然后可以使用字符串函数提取所需的值

  2. # 2 楼答案

    您可以使用getCssValue

    WebElement eleWidth = driver.findElement(By.id("element_id")).getCssValue("width");
    WebElement eleHeight = driver.findElement(By.id("element_id")).getCssValue("height");
    
  3. # 3 楼答案

    请尝试以下代码:

    import org.openqa.selenium.By;
    import org.openqa.selenium.Dimension;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class getSize {
    
        public static void main(String[] args) {
            WebDriver driver = new ChromeDriver();
            driver.get("https://google.com");
            WebDriverWait wait = new WebDriverWait(driver,15);
            wait.until(ExpectedConditions.presenceOfElementLocated(By.className("gsfi")));
            Dimension dimesions=driver.findElement(By.className("gsfi")).getSize();
            System.out.println("Width : "+dimesions.width);
            System.out.println("Height : "+dimesions.height);
        }
    
    }