我需要在控制台中打印属于不同对象(例如国家(超过100个国家/地区))的值,这些对象具有几乎相同的XPath和类。我尝试使用List的XPath,但它没有给我预期的结果。我在一个国家使用了以下代码,但我需要为其他(超过100个)国家/地区打印相同的值
WebElement GDPAndr = driver.findElement(By.xpath("/html/body/div[2]/div[4]/div/div/div[2]/div[2]/div[2]/div/div/div[1]/div/div[2]/span/span/a/span"));
System.out.println(GDPAndr.getText());
WebElement POPT = driver.findElement(By.xpath("/html/body/div[2]/div[4]/div/div/div[2]/div[2]/div[2]/div/div/div[2]/div/div/div/div[2]/span/span/a/span"));
System.out.println(POPT.getText());
WebElement CO2 = driver.findElement(By.xpath("/html/body/div[2]/div[5]/div/div[6]/table/tbody/tr/td[2]/div[2]/div/div/div/div[2]/div/div/div/span[1]"));
System.out.println(CO2.getText());
寻求帮助!感谢
答案 0 :(得分:-1)
使用getAttribute(" value");而不是getText(); 如下所示:
WebElement GDPAndr = driver.findElement(By.xpath("/html/body/div[2]/div[4]/div/div/div[2]/div[2]/div[2]/div/div/div[1]/div/div[2]/span/span/a/span"));
System.out.println(GDPAndr.getAttribute("value"));
WebElement POPT = driver.findElement(By.xpath("/html/body/div[2]/div[4]/div/div/div[2]/div[2]/div[2]/div/div/div[2]/div/div/div/div[2]/span/span/a/span"));
System.out.println(POPT.getAttribute("value"));
WebElement CO2 = driver.findElement(By.xpath("/html/body/div[2]/div[5]/div/div[6]/table/tbody/tr/td[2]/div[2]/div/div/div/div[2]/div/div/div/span[1]"));
System.out.println(CO2.getAttribute("value"));