如何在控制台上打印SVG标签元素



我想在控制台上打印我的元素

WebElement element = driver.findElement(
    By.xpath("//*[name()='svg']/*[name()='rect' and @height='40']")
);
String val = element.getText();
System.out.println("val");

可能有多个问题,例如

  1. xpath不匹配任何元素
  2. 等待问题元素未加载
  3. 字符串变量不应以syso中的双引号传递

以下代码正在工作和打印大约50个值,即公司名称和AVG工资

public void naukriTest() throws InterruptedException{
    driver.get("https://www.naukri.com/nlogin/login");
    driver.findElement(By.id("usernameField")).sendKeys("validemail");
    driver.findElement(By.id("passwordField")).sendKeys("validpassword");
    driver.findElement(By.xpath("//button[@class='waves-effect waves-light btn-large btn-block btn-bold blue-btn']")).click();
    Thread.sleep(3000);
    driver.get("https://careernavigator.naukri.com/sales-executive-retail-careers-in-mahindra-and-mahindra-financial-services-15731");
    Thread.sleep(18000);
    System.out.println(driver.getTitle());
    List<WebElement> simpleTable = driver.findElements(By.xpath("(//*[name()='svg'])[2]//*[name()='text']//*[name()='tspan' and (@dy='4.40625' or @dy='3.609375')]"));
    System.out.println("size is " + simpleTable.size());
    for (int i=0;i<simpleTable.size();i++)
        System.out.println(simpleTable.get(i).getText());
}

请求请不要标记-ve thread.sleep使用情况,因为我只是专注于从dom中找到正确的xpath和打印值。

最新更新