【问题标题】:Selenium program returning NoSuchElementException no matter what element query type I use?无论我使用什么元素查询类型,Selenium 程序都会返回 NoSuchElementException?
【发布时间】: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


    【解决方案1】:

    NoSuchElement 异常发生在运行时网页上不存在用于处理的元素时。此外,您的 findElement 在您的代码中是错误的。请参考下面的代码从您的网页打印23,433.57

    WebElement TxtBoxContent = driver.findElement(By.cssSelector("td > .last:nth-child(1)"));
    System.out.println("Text " +TxtBoxContent.getText());
    

    【讨论】:

    • 一个额外的括号,否则正是我需要的!非常感谢,我花了几个小时试图让这个格式正确!!
    • 您是否愿意accept 回答并从您的最后点按upvote 按钮。
    • 你只是领先我一点哈哈。刚刚做了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 2018-10-07
    • 2016-10-30
    • 1970-01-01
    相关资源
    最近更新 更多