我应该单击网站上的“选择文件”按钮,它应该打开一个窗口,允许我选择文件,但即使我对其进行编码以单击该元素,该窗口也不会打开。
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();
上传后,窗口应该会打开,但事实并非如此。
<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");
输出:
非常感谢。 actions.moveToElement(uploadPhotoBtn).click().perform();工作了
尝试使用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();