如何在硒中打印具有共同css值的元素

时间:2017-06-19 18:53:11

标签: java selenium selenium-webdriver

WebDriver driver = new FirefoxDriver();
driver.get("https://www.ignitionone.com/company/careers/");
driver.manage().window().maximize();

Thread.sleep(2000);

driver.findElement(By.cssSelector("button.teal")).click();      

Thread.sleep(2000);
String s2 =driver.findElement(By.cssSelector("#board_title")).getText();

List<WebElement>d_details = driver.findElements(By.cssSelector(".level-0"));
    for(int i=0; i<d_details.size();i++){
    WebElement element = d_details.listIterator();
    String innerhtml = element.getAttribute("innerHTML");

    System.out.println("Available openings  are" + innerhtml);
}


System.out.println("The title is " + s2);

driver.quit();

这是我的代码。我正在尝试打印网页中不同区域的可用职位空缺。有人可以帮助了解在这里挣扎的事情。

1 个答案:

答案 0 :(得分:2)

此行上有类型转换问题

WebElement element = d_details.listIterator();

迭代元素的更好方法是:

List<WebElement> results = driver.findElements(By.cssSelector(".level-0"));
for (WebElement result: results) {
    String innerhtml = result.getAttribute("innerHTML");
    System.out.println("Available openings  are" + innerhtml);
}

请注意,您可能还遇到了计时问题。您应该使用显式等待命令替换Thread.sleep()次调用,请查看以下主题: