【发布时间】:2020-07-21 10:29:47
【问题描述】:
我有一个股票数据站点,我正在尝试使用 Selenium 来解析数据。我觉得我真的很接近,但我的代码中的某些东西导致程序返回 NoSuchElementException。我可以使用 Chrome 开发者控制台中所需的元素对页面 (https://www.cnbc.com/quotes/?symbol=.DJI) 进行 JS 查询,如下所示:
document.getElementsByClassName("last original ng-binding")
但是,在我的基于 Java 的 Selenium 程序中执行类似的查询以按类名查找元素会返回此异常。当我使用与 JS 查询相同的类名时,为什么会这样?我也尝试过通过 xpath 和 css 进行查询,但也出现了类似的错误。这是我的代码:
//Built in Java 1.7 due to Selenium compatabilities
//Import packages
import java.io.File;
import java.util.Scanner;
import org.openqa.selenium.By;
import org.openqa.selenium.By.ByClassName;
import org.openqa.selenium.InvalidArgumentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Tester {
//Instantiate common variables
WebDriver driver;
String url;
Scanner input;
boolean error;
int uses = 0;
public void invokeBrowser() {
try {
//Point Selenium to Chrome Driver file
File file = new File("C:\\Users\\zrr81\\eclipse-workspace\\WebScraper\\Selenium\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
//Initialize Chrome driver and perform maintenance functions
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
uses ++;
//Execute parseData code
parseData();
} catch (Exception e) {
e.printStackTrace();
}
}
//Try to connect, include catch for typos/errors
public void parseData() {
try {
input = new Scanner(System.in);
System.out.println("Enter the url you would like to connect to: ");
String url = input.nextLine();
driver.get(url);
elementLocator();
}
//Specific catch for an invalid site (e.g. a dead link)
catch(InvalidArgumentException e) {
System.out.println("He's dead Jim! :/");
}
//Catch for all other exceptions
catch(Exception ex) {
System.out.println("Check your syntax partner!");
}
}
public void elementLocator() {
try {
//driver.findElement(By.linkText("DJIA</a>")).click();
String stock = driver.findElement(By.className("last original ng-binding")).toString();
System.out.println(stock);
//Catch specific exception for html element not found
} catch (NoSuchElementException e) {
System.out.println("Selected element not found");
error = true;
//General exception catch
} catch (Exception ex) {
ex.printStackTrace();
}
finally {
if (error==true) {
closeBrowser();
}
}
}
public void closeBrowser() {
//If only 1 window is open, close the window
if(uses == 1) {
driver.close();
}
//Otherwise, close the whole browser
else {
driver.quit();
}
}
//Invoke methods
public static void main(String[] args) {
Tester myObj = new Tester();
myObj.invokeBrowser();
}
}
【问题讨论】:
标签: java html selenium google-chrome web-scraping