使用Selenium在Java中并排打印季节和三个指针,而不是一个低于另一个。我该怎么做?
好的,今天的最后一个问题,因为这是我项目的最后一步 我设法在各个页面间导航,以到达所需的页面。然后我需要从表格中打印出季节和3PA。我将Java与Selenium和Maven一起使用。我尝试了嵌套的使用Selenium在Java中并排打印季节和三个指针,而不是一个低于另一个。我该怎么做?,java,selenium,maven,selenium-webdriver,web-scraping,Java,Selenium,Maven,Selenium Webdriver,Web Scraping,好的,今天的最后一个问题,因为这是我项目的最后一步 我设法在各个页面间导航,以到达所需的页面。然后我需要从表格中打印出季节和3PA。我将Java与Selenium和Maven一起使用。我尝试了嵌套的for循环,它确实可以正确地并排打印结果(因为我可以使用System.out.printIn(s.getText()+“”+t.getText()),但是季节乘以3。我现在有两个独立的for循环,它可以工作,但结果如下: 2018-19 2019-20 2020-21 7.1 8.9 8.2 201
for
循环,它确实可以正确地并排打印结果(因为我可以使用System.out.printIn(s.getText()+“”+t.getText()),但是季节乘以3。我现在有两个独立的for
循环,它可以工作,但结果如下:
2018-19
2019-20
2020-21
7.1
8.9
8.2
2018-19 7.1
2019-20 8.9
2020-21 8.2
for(int i = 0; i< Seasons.size(); i++){
System.out.println(Seasons.get(i).getText() + "\t" + threePointers.get(i).getText());
}
我希望它是这样的:
2018-19
2019-20
2020-21
7.1
8.9
8.2
2018-19 7.1
2019-20 8.9
2020-21 8.2
for(int i = 0; i< Seasons.size(); i++){
System.out.println(Seasons.get(i).getText() + "\t" + threePointers.get(i).getText());
}
这是我的密码:
public class Scraper {
public static void main( String[] args ) throws IOException
{
System.setProperty("webdriver.chrome.driver", "D:/Chrome/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.basketball-reference.com/");
driver.findElement(ByXPath.xpath("//*[@id='qc-cmp2-ui']/div[2]/div/button[3]")).click();
Scanner my_scan = new Scanner(System.in);
System.out.print("Vpisi ime igralca: ");
String my_text = my_scan.nextLine();
driver.findElement(By.xpath("// [@id='header']/div[3]/form/div/div/input[2]")).sendKeys(my_text);
driver.findElement(ByXPath.xpath("//*[@id='header']/div[3]/form/input[1]")).click();
driver.findElement(ByXPath.xpath("//*[@id='players']/div[1]/div[1]/strong/a")).click();
WebElement element = driver.findElement(By.id("div_per_game"));
List<WebElement> threePointers = element.findElements(By.cssSelector("td[data-stat='fg3a_per_g']"));
List<WebElement> Seasons = element.findElements(By.xpath("//*[@id='per_game']/tbody/tr/th/a"));
for (WebElement s: Seasons)
{
System.out.println(s.getText());
}
for (WebElement t : threePointers)
{
System.out.println(t.getText());
}
}
}
公共类刮板{
公共静态void main(字符串[]args)引发IOException
{
System.setProperty(“webdriver.chrome.driver”,“D:/chrome/chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
驱动程序。获取(“https://www.basketball-reference.com/");
findElement(ByXPath.xpath(“//*[@id='qc-cmp2-ui']/div[2]/div/button[3]”)。单击();
扫描仪my_scan=新扫描仪(System.in);
系统输出打印(“Vpisi ime igralca:”;
String my_text=my_scan.nextLine();
findelelement(By.xpath(“//[@id='header']/div[3]/form/div/div/input[2]”);
findElement(ByXPath.xpath(“//*[@id='header']]/div[3]/form/input[1]”);
findElement(ByXPath.xpath(“//*[@id='players']/div[1]/div[1]/strong/a”))。单击();
WebElement=driver.findElement(By.id(“div_per_game”);
列出三个指针=element.findElements(By.cssSelector(“td[data stat='fg3a_per_g']”);
List seasures=element.findElements(By.xpath(“/*[@id='per_game']]/tbody/tr/th/a”);
对于(WebElement s:季节)
{
System.out.println(s.getText());
}
for(WebElement t:threePointers)
{
System.out.println(t.getText());
}
}
}
这是我从中获取数据的页面和表格(每场比赛表格):
希望你的导师能帮助我。而不是
for (WebElement s: Seasons)
{
System.out.println(s.getText());
}
for (WebElement t : threePointers)
{
System.out.println(t.getText());
}
您可以这样做:
2018-19
2019-20
2020-21
7.1
8.9
8.2
2018-19 7.1
2019-20 8.9
2020-21 8.2
for(int i = 0; i< Seasons.size(); i++){
System.out.println(Seasons.get(i).getText() + "\t" + threePointers.get(i).getText());
}
for(int i=0;i
\t
用于在第一个和第二个元素列表的文本之间添加制表符。考虑到只有在
季节
和三点
列表长度相同的情况下,这才可以在没有额外逻辑的情况下工作