[英]How to upload file in selenium webdriver using PhantomJS in .net?
[英]Cannot upload file using PhantomJs (and Selenium WebDriver)
我正在做的事情:
首先,我的HTML看起來如下:
<form action="http://example.com/upload_photo_iframe.html" preview_div="upload_photo_div" submit_button="submit_btn" upload_field="photo_upload" target="photo_target" enctype="multipart/form-data" method="POST" id="uploadfile" name="uploadfile">
<input type="hidden" value="CSRF_iframe_photo_upload" name="csrfname">
<input type="hidden" value="3350427f0f068509081a09e283607214001b6912843ffb937b934208c91d9041c88faf0e66df4f3898ef202a34b669647f5b3fd9a2122e389acd3f53c33fc88b" name="csrftoken">
<label class="file-upload">
<p id="upload_text">Click Browse to choose a file then click Upload</p>
<input type="file" class="file" id="photo_upload" name="photo_upload" accept="image/*">
</label>
<input type="hidden" value="true" name="makeLargeThumb">
<input type="hidden" value="p" name="size">
<input type="hidden" value="P" name="type">
<input type="hidden" value="5120000" name="MAX_FILE_SIZE">
<input type="hidden" value="0" name="album_id" class="album_id_hidden">
<input type="submit" class="grey_btn" id="upload_btn" value="Upload" name="submit_btn">
<input type="hidden" name="callback" value="document.forms.uploadfile.handleReceive"><input type="hidden" name="fieldName" value="photo_upload"></form>
我發布了整個文件,文件輸入是以下部分:
<input type="file" class="file" id="photo_upload" name="photo_upload" accept="image/*">
我的情況:
我正在使用Selenium WebDriver和PhantomJs(C#)。 在此之前,我使用FireFox而不是PhantomJs,並通過以下方式上傳文件:
_driver.FindElement(photoUploadPath).SendKeys(imagePath);
我的問題:
然而,當我使用PhantomJs時,這會崩潰。
我試圖解決這個問題:
我發現以下主題討論了這個問題: https : //github.com/ariya/phantomjs/issues/10993
我嘗試了提出的解決方案:
((PhantomJSDriver)driver).executePhantomJS("var page = this; page.uploadFile('input[type=file]', '/path/to/file');");
但是,雖然這不會引發錯誤,但它不會上傳文件。
經過廣泛搜索后,我唯一的選擇是在這里問,希望有人可以幫助我。
在我看來,對於許多使用PhantomJs + Selenium的人來說這是一個常見的問題,這讓我想知道為什么這個bug還沒有修復。
剛剛面對同樣的問題並解決了它。 PhantomJS 2.0中的文件上傳功能已中斷。 有關更多信息,請查看此主題https://github.com/ariya/phantomjs/issues/12506 。
所以一般來說你應該自定義構建PhantomJS瀏覽器(或等待官方修復)並使用上面已經提到的腳本:
((PhantomJSDriver)driver).executePhantomJS("var page = this; page.uploadFile('input[type=file]', '/path/to/file');");
請注意,線程中已經有一些自定義構建(我在那里為Windows進行了自定義構建)。
看看我在這里的答案它涉及過去我處理非瀏覽器對話框(例如文件上傳)時的運氣過程。
雖然我鏈接到的問題是關於Java,但我的答案中的代碼實際上是C#。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.