2017-09-25 98 views
1

嘗試使用selenium webDriver將圖像上傳到dropzone。已經意識到它爲站點dropzonejsdotcom,但如果我嘗試使用腳本爲reactJS dropzone(http://reactdropzone.azurewebsites.net/example/)我收到一條消息:「未知錯誤:Dropzone未定義」。ReactJS dropzone通過webdriver上傳文件

JavascriptExecutor executor; 

public ReactDrpzn() { 
    Configuration.browser = "chrome"; 
    this.executor = (JavascriptExecutor) WebDriverRunner.getWebDriver(); 
} 
private By dropzone = By.cssSelector(".filepicker.dropzone.dz-clickable"); 

@Test 
public void drzon() throws IOException, InterruptedException { 
    open("http://reactdropzone.azurewebsites.net/example"); 
    $(dropzone).hover(); 
    String script = "var myZone, blob, base64Image; myZone = Dropzone.forElement('.filepicker.dropzone.dz-clickable');" + 
      "base64Image = 'iVBORw0KGgoAAAANSUhEUgAAAO0AAABQCAYAAAD1GfIkAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAEGSURBVHhe7dPBCcAwAAMxp/vv3ObRJQ4kMJ7gzrb3Doh4/gciRAsxooUY0UKMaCFGtBAjWogRLcSIFmJECzGihRjRQoxoIUa0ECNaiBEtxIgWYkQLMaKFGNFCjGghRrQQI1qIES3EiBZiRAsxooUY0UKMaCFGtBAjWogRLcSIFmJECzGihRjRQoxoIUa0ECNaiBEtxIgWYkQLMaKFGNFCjGghRrQQI1qIES3EiBZiRAsxooUY0UKMaCFGtBAjWogRLcSIFmJECzGihRjRQoxoIUa0ECNaiBEtxIgWYkQLMaKFGNFCjGghRrQQI1qIES3EiBZiRAsxooUY0UKMaCFGtBAjWkjZPk/PAZ8fwt/rAAAAAElFTkSuQmCC';" + 
      "function base64toBlob(r,e,n){e=e||\"\",n=n||512;for(var t=atob(r),a=[],o=0;o<t.length;o+=n){for(var l=t.slice(o,o+n),h=new Array(l.length),b=0;b<l.length;b++)h[b]=l.charCodeAt(b);var v=new Uint8Array(h);a.push(v)}var c=new Blob(a,{type:e});return c}" + 
      "blob = base64toBlob(base64Image, 'image/png');" + 
      "blob.name = 'file.png';" + 
      "myZone.addFile(blob);"; 

    executor.executeScript(script); 
    Thread.sleep(3000);//смотрим результат 
} 

U可以從這裏https://github.com/SaneQ/Drpzn

+0

原因是這會不會在瀏覽器控制檯的工作也。 DropZone對象位於反應環境內部,並且該對象在主窗口​​中不可用。所以你需要以某種方式首先暴露或獲得這個對象 –

回答

1

基本上,兩個步驟:

  • 找到元素:
  • 發鍵入要上傳文件的位置。

的JavaScript:

const {Builder, By, Key, until} = require('selenium-webdriver'); 

let driver = new Builder() 
    .forBrowser('chrome') 
    .build(); 

driver.get('http://reactdropzone.azurewebsites.net/example/'); 
driver.findElement(By.xpath('.//input[@type="file"]')).sendKeys('full_path_to_your_file.png'); 

的Python:

from selenium import webdriver 

driver = webdriver.Chrome() 
driver.get('http://reactdropzone.azurewebsites.net/example/') 
input_field = driver.find_element_by_xpath('.//input[@type="file"]') 
input_field.send_keys('full_path_to_your_file.png') 
0

導入行家所以,我發現簡單的方法反應懸浮窗上載圖片。 反應。在DOM(正文>輸入) 創建隱伏輸入此後,我只是用硒的SendKeys( 「pathToImg」)