gpt4 book ai didi

java - Selenium:无法打印存储的 IMDB 热门电影和评级列表中的所有值

转载 作者:行者123 更新时间:2023-11-30 10:27:38 28 4
gpt4 key购买 nike

我无法打印列表中的所有值。

完整代码如下:

public class TestCase1{

private static XSSFWorkbook wb;
private static XSSFSheet sh;
private static File file;
static int i=0;

public static void main(String args[]) throws Exception
{
file = new File("D:\\Eclipse and workspace\\workspace\\SeleniumPractice\\IMDBTestCase1.xlsx");
FileInputStream fIs = new FileInputStream(file);
wb = new XSSFWorkbook(fIs);
sh = wb.getSheet("Sheet1");
sh.createRow(i);

System.setProperty("webdriver.gecko.driver", "D:\\Eclipse and workspace\\eclipse\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

driver.get("http://www.imdb.com");

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.id("navTitleMenu"))).build().perform();

Thread.sleep(5000);

driver.findElement(By.xpath("//div[@id='navMenu1']/div[2]/ul[1]/li[6]/a")).click();

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@class='chart full-width']/thead/tr/th[2]")));

WebElement elem1 = driver.findElement(By.xpath("//table[@class='chart full-width']/thead/tr/th[2]"));
String title = elem1.getText();
write(0,0,title);

WebElement elem2 = driver.findElement(By.xpath("//table[@class='chart full-width']/thead/tr/th[3]"));
String rating = elem2.getText();
write(0,1,rating);
i++;

List <WebElement> elements = driver.findElements(By.className("lister-list"));
for(WebElement element : elements)
{
String str1 = element.findElement(By.xpath("//tbody[@class='lister-list']/tr["+i+"]/td[2]")).getText();
System.out.println(str1);
//write(i,0,elem3.getText());

String str2 = element.findElement(By.xpath("//tbody[@class='lister-list']/tr["+i+"]/td[3]")).getText();
System.out.println(str2);
//write(i,1,elem4.getText());

i++;
}
System.out.println("Success");

wb.close();
}

public static void write(int row, int col, String val) throws Exception
{
FileOutputStream fos = new FileOutputStream(file);
sh.createRow(row+1);
sh.getRow(row).createCell(col).setCellValue(val);
wb.write(fos);
}

现在在下面的部分,

List <WebElement> elements = driver.findElements(By.className("lister-list"));
for(WebElement element : elements)
{
String str1 = element.findElement(By.xpath("//tbody[@class='lister-list']/tr["+i+"]/td[2]")).getText();
System.out.println(str1);
//write(i,0,elem3.getText());

String str2 = element.findElement(By.xpath("//tbody[@class='lister-list']/tr["+i+"]/td[3]")).getText();
System.out.println(str2);
//write(i,1,elem4.getText());

i++;
}

这里的输出只打印了这个:

1. The Shawshank Redemption (1994)
9.2
Success

我想打印所有的电影名称和评分,但我不确定为什么它只打印第一部。此外,如果我使用下面的 for 循环,它将打印所有名称和评级,但我希望将它们分开,因为我想将它们存储在 excel 文件中。

for(WebElement element : elements)
{
System.out.println(element.getText());
}

PS:我在这里真正要做的是将电影名称及其评级存储在 excel 文件中。

最佳答案

如果你调试你的元素列表,你会发现它只有 1 个元素。返回多个元素需要在tbody中指定tr元素。

我更喜欢通过 CSS 选择器获取元素,因为我发现它更容易编写。这将得到你想要的......

List <WebElement> elements = driver.findElements(By.cssSelector(".lister-list > tr"));

for(WebElement element : elements)
{
String title = element.findElement(By.cssSelector(".titleColumn")).getText();
System.out.println(title);

String rating = element.findElement(By.cssSelector(".ratingColumn.imdbRating")).getText();
System.out.println(rating);

}

更新:这里是使用 XPATH 的实现。

List <WebElement> elements = driver.findElements(By.xpath("//tbody[@class='lister-list']//tr"));

for(WebElement element : elements)
{
String title = element.findElement(By.xpath("td[@class='titleColumn']")).getText();
System.out.println(title);

String rating = element.findElement(By.xpath("td[contains(@class, 'ratingColumn') and contains(@class, 'imdbRating')]")).getText();
System.out.println(rating);

}

关于java - Selenium:无法打印存储的 IMDB 热门电影和评级列表中的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45241780/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com