[英]When element is 'display:none', how can I upload a image file?
我想通過Selenium上傳文件。 頁面網址如下。
<div class="async-upload__thumb item-image__area">
<div class="fab-dialog__thumb-drop-zone async-upload__thumb-drop-zone">
<p class="async-upload__thumb-msg" display="none">SELECT IMAGE</p>
<input type="file" accept="image/jpeg,image/gif,image/png" multiple="" style="display:none">
</div>
</div>
但是輸入元素為display="none"
所以我通過此代碼上傳了文件。
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("test.jpg");
我有這樣的錯誤:
Element is not currently visible and so may not be interacted with
Command duration or timeout: 24 milliseconds
如何上傳圖片文件?
我不確定這是否行得通,但是您可以刪除樣式屬性。
WebElement element = driver.findElement(By.xpath("//input[@type='file']"))
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].removeAttribute('style')", element);
您可能需要再次獲取Element,因為其屬性已更改,或者您可能會獲取StaleElementException
在上面的代碼之后,以下行應該可以工作。
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("test.jpg");
我用這段代碼解決了這個問題。 它刪除了display:none表單輸入元素。
// Remove display : none
// JavascriptExecutor js = (JavascriptExecutor) driver;
// js.executeScript("$(\"input[type='file']\").css('display','')");
然后出現選擇圖像按鈕和sendKeys()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.