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

时间:2019-05-24 08:47:31

标签: java selenium

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

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

String val = element.getText();
System.out.println("val");

1 个答案:

答案 0 :(得分:0)

可能有多个问题,例如

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

以下代码正在工作,并从表中打印大约50个值,即公司名称和平均工资

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,因为我只是专注于找到正确的xpath并从DOM中打印值。