java - 复制记事本中的所有内容并粘贴到网页的文本区域中

标签 java selenium

是否可以使用Java/Selenium复制记事本文件中的所有内容(Ctrl+A)并粘贴到网页的Textarea中?

最佳答案

解决方法如下:

将文件内容复制到网页文本框中的代码:此代码将一个文本文件的内容复制并粘贴到 google.com 上的 Google 搜索文本框中,然后点击搜索按钮。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class CopyFileToTextbox {

public static void main(String[] args) {

    File inFile = new File("D:\\path\\to\\notepad\\file\\TextFile1.txt");
    StringBuilder targetString = new StringBuilder("");
    try {
        FileReader fr = new FileReader(inFile);
        BufferedReader br = new BufferedReader(fr);

        String s = null;
        while ((s = br.readLine()) != null) {
            targetString.append(s);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com");
    WebDriverWait wait = new WebDriverWait(driver,10);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.name("q")));
    driver.findElement(By.name("q")).sendKeys(targetString);
    driver.findElement(By.name("btnG")).click();
    driver.quit();


}

}

关于java - 复制记事本中的所有内容并粘贴到网页的文本区域中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37478476/

相关文章:

c# - 使用 IE 浏览器运行测试用例时 C# 中的 Selenium

java - Selenium 错误: "org.openqa.selenium.SessionNotCreatedException: session not created from tab crashed" after update electron engine to 9. 0.3

xpath - Selenium IDE 和 xpath - 在表中查找文本/行并选择单选框

python - 页面未在 Windows 上使用 Python 加载到 Selenium WebDriver 中

java - 无法使用 Swing 登录

java - 使用 rs.next() 获取所有数组元素

java - org.springframework.web.client.HttpClientErrorException : 400 Bad Request for PUT

python - 无法在 python 中使用 Chromedriver 和 selenium 消除音频错误

java - 平面文本文件解析

java - 进程输入流中丢失的特殊字符