有没有办法使用无头铬中的硒上传文件?

时间:2019-01-24 08:30:17

标签: java selenium google-chrome-headless serenity-bdd

我正在创建一个脚本,该脚本要求我上传文件,所以我写类似以下内容的

    @FindBy(css = "div[title='Add an attachment'] button")
    private WebElementFacade FILE_UPLOAD_BUTTON;

    Path path = Paths.get(System.getProperty("user.dir"));

    withTimeoutOf(20, TimeUnit.SECONDS).waitFor(ExpectedConditions.visibilityOf(FILE_UPLOAD_BUTTON));
    FILE_UPLOAD_BUTTON.click();

    filePath = Paths.get(path.toString(), "FolderName", "ActualFileName.pdf");

    StringSelection fullPath = new StringSelection(filePath.toString());
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(fullPath, fullPath);

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    pause(2000);

它工作正常,但在无头镀铬中则不能。关于如何在Headless chrome中上传文件的任何想法? TIA。

编辑:添加了inquiry to serenity,wakaleo怀疑Robot类是否可以在无头的chrome上运行,因为它与真实的UI交互。我还尝试了他的建议,使用chord org.openqa.selenium.Keys;actions org.openqa.selenium.interactions.Actions;之类的标准硒行动,但它们仍然没有起作用

4 个答案:

答案 0 :(得分:1)

您可以使用AutoIt及其编辑器在硒中上传文件

1。您需要安装Autoit及其脚本编辑器

我已共享链接,您可以下载并使用它

https://www.autoitscript.com/site/autoit/downloads/

  1. 您需要创建自动文件,并需要传递文件位置和一些脚本 喜欢并给文件命名,就像我给了File Upload.au3一样,.au3扩展名自动出现

    ControlFocus("Open","","Edit1")
    ControlSetText("Open","","Edit1","E:\AutoIT\id.pdf")
    ControlClick("Open","","Button1")
    
  2. 您需要右键单击文件upload.au3并进行编译,然后它将创建执行文件File Upload.exe

  3. 然后,您需要在硒中指定需要执行和上传文件的位置,就像在我的项目中一样,单击“上传”按钮后,我正在使用Runtime.getRuntime().exec(Globals.PROG_FILEUPLOAD);

  4. 执行该文件

其中Global.PROG_FILEUPLOAD是文件Upload.exe之类的路径

PROG_FILEUPLOAD= "E:/AutoIT/File Upload.exe"

我还共享了链接,以供您如有疑问时使用的参考

https://www.guru99.com/use-autoit-selenium.html

答案 1 :(得分:1)

它不起作用,因为您使用的是Robot类,因为浏览器始终不可见,所以它对于无头执行并不理想。

确保您的上传元素可见。

之后,使用以下内容上传:

driver.findElement(By.id("uploadElement")).sendKeys("path/to/file");

答案 2 :(得分:0)

使用以下代码以无头模式上传文件:

    ChromeOptions options = new ChromeOptions();
            options.addArguments("--headless");
            WebDriver driver = new ChromeDriver(options);
            driver.get("http://nervgh.github.io/pages/angular-file-upload/examples/simple/");
            driver.findElement(By.xpath("(//input[@uploader='uploader'])[2]")).sendKeys("C:\\NotBackedUp\\Python\\selenium-2.7.0\\py\\selenium\\selenium.py");
// Then click on some upload button

在sendKeys()方法中给出要上传的文件的确切完整路径。

答案 3 :(得分:0)

是的,您可以使用 sendkeys 通过无头 chrome 上传文件。

  • 此处的关键点是找出合适的元素。
  • sendkeys 与输入标签完美配合(文件作为上传文件的类型)。
  • 找出输入标签引用是否包含在其他标签中
  • 看看下面的例子
<p-fileupload id="file_upload_id">
   <div >
      <span>
        <span>Choose file</span>
        <input type="file" accept=".csv">
      </span>
   </div>
</p-fileupload>
  • 使用 xpath 查找元素
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.WebElement;

@FindBy(xpath="//[@id=\"file_upload_id\"]//following::input[@type=\"file\"]")
@CacheLookup
private WebElement fileupload;
  • 设置无头 chrome 驱动程序
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public  static void initialization() {  
   String browsername=prop.getProperty("browser");
    if(browsername.equals("chrome")) 
    { 
     System.setProperty("webdriver.chrome.driver","...//path-to-chrome-driver//Drivers//chromedriver");
     ChromeOptions chromeOptions = new ChromeOptions();
     chromeOptions.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors","--disable-extensions","--no-sandbox","--disable-dev-shm-usage");
     driver=new ChromeDriver(chromeOptions);
   }

  • 使用 sendkey 发送文件
String userDir = System.getProperty("user.dir");
String sep = System.getProperty("file.separator");
String path=userDir + sep + "Files_dir_name" + sep + "sample.csv";
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
fileupload.sendKeys(value);

注意:userDir - 项目运行的根目录,文件分隔符 - 解决跨操作系统的文件路径问题

相关问题