要在此网址的字段中上传图片:http://demoqa.com/registration/ 我使用了以下代码,适用于Chrome但不适用于Firefox。我可以知道原因和解决方案吗?
WebDriver driver;
System.setProperty("webdriver.gecko.driver","C:\\Users\\abc\\Desktop\\Selenium\\geckodriver-v0.17.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://demoqa.com/registration/");
WebElement elementUpload=driver.findElement(By.xpath("//*[@id='profile_pic_10']"));
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(elementUpload));
elementUpload.sendKeys("D:\\roboraid.jpg");
此处显示的错误是:
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: File not found: C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg
答案 0 :(得分:0)
使用带有显式等待的xpath尝试此代码:
WebDriver driver = new FirefoxDriver();
driver.get("http://demoqa.com/registration/");
driver.manage().window().maximize();
WebElement elementUpload=driver.findElement(By.xpath("//*[@id='profile_pic_10']"));
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(elementUpload));
elementUpload.sendKeys("D:\\1504857398686.png");
答案 1 :(得分:0)
尝试替换文件位置中的空格
来自
C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg
到
C:\Users\Public\Pictures\Sample_Pictures\Chrysanthemum.jpg
希望这有效
答案 2 :(得分:0)
将xpath
更改为以下版本,然后尝试。它对我有用。
//input[@name='profile_pic_10' and @type='file']
我可以知道您尝试过的浏览器和环境详细信息吗?
答案 3 :(得分:0)
实际上这是firefox的geckodriver的问题
参考: -
https://github.com/mozilla/geckodriver/issues/659
您需要更新geckodriver
OR
使用chromedriver如下
System.setProperty("webdriver.chrome.driver", "D:\\Workspace\\StackOverlow\\src\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver( );
driver.get("http://demoqa.com/registration/");
driver.manage().window().maximize();
((JavascriptExecutor) driver).executeScript("scroll(0,300)");
WebElement elementUpload=driver.findElement(By.xpath("//*[@id='profile_pic_10']"));
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(elementUpload));
elementUpload.sendKeys("D:"+File.separator+"22.jpeg");