Java 无论我使用什么元素查询类型,Selenium程序都会返回NoTouchElementException?
我有一个股票数据站点,我正试图使用Selenium解析其中的数据。我觉得我真的很接近了,但是我的代码中的某些东西导致程序返回NoTouchElementException。我可以从Chrome开发者控制台使用我想要的元素对页面()进行JS查询,如下所示:Java 无论我使用什么元素查询类型,Selenium程序都会返回NoTouchElementException?,java,html,selenium,google-chrome,web-scraping,Java,Html,Selenium,Google Chrome,Web Scraping,我有一个股票数据站点,我正试图使用Selenium解析其中的数据。我觉得我真的很接近了,但是我的代码中的某些东西导致程序返回NoTouchElementException。我可以从Chrome开发者控制台使用我想要的元素对页面()进行JS查询,如下所示: document.getElementsByClassName("last original ng-binding") 但是,在基于Java的Selenium程序中执行类似的查询,以按类名查找元素,会返回此异常。为什么我使用与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();
}
}
//由于Selenium的兼容性,内置于Java 1.7中
//导入包
导入java.io.File;
导入java.util.Scanner;
导入org.openqa.selenium.By;
导入org.openqa.selenium.By.ByClassName;
导入org.openqa.selenium.InvalidArgumentException;
导入org.openqa.selenium.NoSuchElementException;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.chrome.ChromeDriver;
公共类测试员{
//实例化公共变量
网络驱动程序;
字符串url;
扫描仪输入;
布尔误差;
int=0;
public void invokeBrowser(){
试一试{
//指向Chrome驱动程序文件
File File=new文件(“C:\\Users\\zrr81\\eclipse workspace\\WebScraper\\Selenium\\chromedriver.exe”);
System.setProperty(“webdriver.chrome.driver”,file.getAbsolutePath());
//初始化Chrome驱动程序并执行维护功能
驱动程序=新的ChromeDriver();
driver.manage().deleteAllCookies();
使用++;
//执行解析数据代码
解析数据();
}捕获(例外e){
e、 printStackTrace();
}
}
//尝试连接,包括输入错误/错误的捕获
公共数据(){
试一试{
输入=新扫描仪(System.in);
System.out.println(“输入您要连接到的url:”;
字符串url=input.nextLine();
获取(url);
元素定位器();
}
//无效站点的特定捕获(例如死链接)
捕获(无效argumentexception e){
System.out.println(“他死了,吉姆!:/”;
}
//捕获所有其他异常
捕获(例外情况除外){
System.out.println(“检查语法伙伴!”);
}
}
公共void元素定位器(){
试一试{
//driver.findElement(By.linkText(“DJIA”))。单击();
String stock=driver.findElement(By.className(“最后一个原始ng绑定”)).toString();
系统输出打印项次(库存);
//未找到html元素的捕获特定异常
}捕获(无接触元素例外e){
System.out.println(“未找到所选元素”);
错误=真;
//一般异常捕获
}捕获(例外情况除外){
例如printStackTrace();
}
最后{
如果(错误==true){
closeBrowser();
}
}
}
公共浏览器(){
//如果只有一个窗口打开,请关闭该窗口
如果(使用==1){
driver.close();
}
//否则,请关闭整个浏览器
否则{
driver.quit();
}
}
//调用方法
公共静态void main(字符串[]args){
测试器myObj=新测试器();
myObj.invokeBrowser();
}
}
运行时网页上不存在要处理的元素时,将发生NoTouchElement异常。此外,您的findElement在代码中是错误的。请参考以下代码从您的网页打印23433.57
WebElement TxtBoxContent = driver.findElement(By.cssSelector("td > .last:nth-child(1)"));
System.out.println("Text " +TxtBoxContent.getText());
一个额外的括号,但在其他方面正是我所需要的!非常感谢你,我花了好几个小时试图把这个格式设置正确!!请你回答,然后从你这边按向上投票按钮。你只是比我领先了一点。哈哈。就这么做了!