org.openqa.selenium.InvalidArgumentException:无效参数对于以下代码
public class Sample {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\Chrome\New folder\chromedriver_win32 (3)\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://cgi-lib.berkeley.edu/ex/fup.html");
System.out.println(driver.getPageSource());
driver.findElement(By.name("upfile")).click();
}
看起来无法在该web元素上触发.click
,从而导致错误。
如果你的最终目标是上传一个文件,你可以使用以下代码:
删除此:
driver.findElement(By.name("upfile")).click();
并替换为:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type='file']"))).sendKeys("C:\Users\your file loaction\Downloads\ASCFTR-5404.docx");
您可以使用sendKeys
传递文件路径,然后单击按钮
System.setProperty("webdriver.chrome.driver", "D:\Chrome\New folder\chromedriver_win32 (3)\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://cgi-lib.berkeley.edu/ex/fup.html");
System.out.println(driver.getPageSource());
driver.findElement(By.name("upfile")).sendKeys("filePath.png");
driver.findElement(By.xpath("//input[@type='submit']")).click();