【问题标题】:Can't get text with Selenium无法使用 Selenium 获取文本
【发布时间】:2020-11-29 20:43:45
【问题描述】:

我对 Java 中的 Selenium WebDriver 有疑问。当我使用此代码(不使用element.click();)时,它可以工作:

public static void main(String[] args) {
    try {
        File salida= new File("salidas/Salida.txt");
        FileWriter fw = new FileWriter(salida);
        PrintWriter volcado = new PrintWriter(fw);

        System.setProperty("webdriver.chrome.driver", "path to\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://ranking-empresas.eleconomista.es/REPSOL-PETROLEO.html");

        String name = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[1]/td[2]")).getText();
        String obj_soc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[2]/td[2]")).getText();
        String direcc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[3]/td[2]")).getText();
        String loc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[4]/td[2]")).getText();
        String tel = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[5]/td[2]")).getText();
        String url = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[8]/td[2]")).getText();
        String actividad = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[9]/td[2]")).getText();

        volcado.print(name + " " + obj_soc + " " + direcc + " " + loc + " " + tel + " " + url + " " + actividad);
        volcado.close();
        driver.close();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

但是当我想通过上一页使用element.click(); 访问时,问题就来了:

    System.setProperty("webdriver.chrome.driver", "path to\\chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    driver.get("http://ranking-empresas.eleconomista.es/ranking_empresas_nacional.html");

    WebElement element = driver.findElement(By.xpath("//*[@id=\"tabla-ranking\"]/table/tbody/tr[1]/td[7]/a"));
    element.click();

    String name = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[1]/td[2]")).getText();
    String obj_soc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[2]/td[2]")).getText();
    String direcc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[3]/td[2]")).getText();
    String loc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[4]/td[2]")).getText();
    String tel = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[5]/td[2]")).getText();
    String url = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[8]/td[2]")).getText();
    String actividad = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[9]/td[2]")).getText();

    volcado.print(name+" "+obj_soc+" "+direcc+" "+loc+" "+tel+" "+url+" "+actividad);
    volcado.close();

    driver.close();
}
catch(Exception e){
    e.printStackTrace();
}}

Selenium 打开浏览器和页面,但我的变量没有得到 XPath 表达式的文本。

【问题讨论】:

    标签: java selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    在您尝试获取文本时,该数据尚未出现在页面上。等待数据再读取,应该没问题:

    WebDriver driver = new ChromeDriver();
    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    driver.get("http://ranking-empresas.eleconomista.es/ranking_empresas_nacional.html");
    
    driver.findElement(By.xpath("//*[@id=\"tabla-ranking\"]/table/tbody/tr[1]/td[7]/a")).click();
    
    // Wait for the data to be present
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("business-profile")));
    
    String name = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[1]/td[2]")).getText();
    String obj_soc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[2]/td[2]")).getText();
    String direcc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[3]/td[2]")).getText();
    String loc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[4]/td[2]")).getText();
    String tel = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[5]/td[2]")).getText();
    String url = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[8]/td[2]")).getText();
    String actividad = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[9]/td[2]")).getText();
    
    volcado.print(name + " " + obj_soc + " " + direcc + " " + loc + " " + tel + " " + url + " "+actividad);
    volcado.close();
    

    但是,更简洁的替代方法是使用单个 XPath 表达式获取所有单元格:

    driver.get("http://ranking-empresas.eleconomista.es/ranking_empresas_nacional.html");
    
    driver.findElement(By.xpath("//*[@id=\"tabla-ranking\"]/table/tbody/tr[1]/td[7]/a")).click();
    
    // Wait for the data to be present
    List<WebElement> cells = wait.until(
      ExpectedConditions.presenceOfAllElementsLocatedBy(
        By.xpath("//h3[.='Datos comerciales de REPSOL PETROLEO SA']/following::tbody[1]/tr/td[2]")));
    
    String name = cells.get(0).getText();
    String obj_soc = cells.get(1).getText();
    String direcc = cells.get(2).getText();
    String loc = cells.get(3).getText();
    String tel = cells.get(4).getText();
    String url = cells.get(7).getText();
    String actividad = cells.get(8).getText();
    

    【讨论】:

      猜你喜欢
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      相关资源
      最近更新 更多