首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Selenium将数据保存到文件中

如何使用Selenium将数据保存到文件中
EN

Stack Overflow用户
提问于 2018-08-06 15:14:08
回答 2查看 5.5K关注 0票数 3
代码语言:javascript
复制
public static void main(String[] args) throws IOException {

    System.setProperty("src/driver/chromedriver", "G:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.premierleague.com/tables");

    WebElement table;

    table = driver.findElement(By.xpath("//*[@id=\"mainContent\"]/div/div[1]/div[3]/div/div"));
    String dataoutput;
    dataoutput = table.getText();
    System.out.println(dataoutput);

    String csvOutputFile = "table.csv";

    File filedata = new File("src/main/table.csv");
    if (filedata.exists() && !filedata.isFile()) {
        FileWriter writecsv = new FileWriter("src/main/table.csv");
        String datas = dataoutput;
        writecsv.append(dataoutput)
    }
}

这是我的代码,但它没有将数据保存到文件。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-06 15:55:42

以下代码适用于我:

代码语言:javascript
复制
    driver.get("https://www.premierleague.com/tables");

    WebElement table;

    WebDriverWait wait = new WebDriverWait(driver, 30);
    table = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"mainContent\"]/div/div[1]/div[3]/div/div")));
    String dataoutput;
    dataoutput = table.getText();
    System.out.println(dataoutput);

    String csvOutputFile = "table.csv";

    try(FileWriter writecsv = new FileWriter("src/main/table.csv")) {
        writecsv.append(dataoutput);
    }
  1. 当我们在这里创建一个新文件时,文件检查将被删除。
  2. 使用WebDriverWait添加显式等待,以等待表元素的显示。
  3. 将FileWriter保存在try块中,因为它为我提供了编译问题。这个语法的好处是它自动关闭了fileWriter对象。
票数 1
EN

Stack Overflow用户

发布于 2018-08-06 15:56:28

由于以下原因,数据未保存在文件中。请使用以下修改(工作)代码。

  1. 如果条件是添加!filedata.isFile(),并且它始终是false.So,则需要将其更改为filedata.isFile()
  2. FileWriter对象未关闭,执行追加操作后需要关闭
  3. 作为最佳实践,在启动URL.otherwise后添加一些等待,元素将无法正确找到,NoSuchElementException异常将被抛出。

代码:

代码语言:javascript
复制
public static void main(String[] args) throws IOException {

    System.setProperty("src/driver/chromedriver", "G:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.premierleague.com/tables");

    //Wait is addeded for the Page Load completion 
    WebDriverWait wait=new WebDriverWait(driver,20);
    wait.until(ExpectedConditions.titleContains("Premier"));


    WebElement table;

    table = driver.findElement(By.xpath("//*[@id=\"mainContent\"]/div/div[1]/div[3]/div/div"));

    String dataoutput = table.getText();
    System.out.println(dataoutput);


    File filedata = new File("src/main/table.csv");

    //Not Equal condition is modified
    if (filedata.exists() && filedata.isFile()) {
        FileWriter writecsv = new FileWriter(filedata);
        String datas = dataoutput;
        writecsv.append(dataoutput)
        writecsv.close();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51710799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档