我應該單擊網站上的“選擇檔案”按鈕,它應該打開一個視窗,允許我選擇檔案,但即使我對其進行編碼以單擊元素,該視窗也不會打開。
System.setProperty("webdriver.chrome.driver","C:\Users\shash\eclipse-wo");
WebDriver driver=new ChromeDriver();
driver.navigate().to("http://the-internet.herokuapp.com/upload");
Thread.sleep(5000);
WebElement uploadPhotoBtn = driver.findElement(By.xpath("//input[@id='file-upload']"))
uploadPhotoBtn.click();
上傳后,視窗應該打開,但不是。
uj5u.com熱心網友回復:
<input id="file-upload" type="file" name="file">
HTML 標記型別是input您可以直接將檔案位置發送到element.
代碼:
driver = new ChromeDriver();
driver.navigate().to("http://the-internet.herokuapp.com/upload");
Thread.sleep(5000);
WebElement uploadPhotoBtn = driver.findElement(By.xpath("//input[@id='file-upload']"));
uploadPhotoBtn.sendKeys("C:\\Sample.json");
輸出:

uj5u.com熱心網友回復:
嘗試使用Actions,如下所示:
Imports Required
import org.openqa.selenium.interactions.Actions;
driver.get("http://the-internet.herokuapp.com/upload");
Actions actions = new Actions(driver);
WebElement uploadPhotoBtn = driver.findElement(By.xpath("//input[@id='file-upload']"));
actions.moveToElement(uploadPhotoBtn).click().perform();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/346997.html
