文件上传按钮无法使用 Selenium webdriver

File Upload button is not working using Selenium webdriver

我正在尝试将 jpeg 图片上传到我的应用程序。但在网页上,我们也可以在各自的部分上传其他 jpeg 图像。这里根据 HTML,上传按钮具有为其他上传部分定义的相同属性。

但是,当我检查上传按钮的唯一元素的 xpath 时,我发现了下面的 xpath=//table/tbody/tr[1]/td[1]/div/div/div/input[@class='bttnUpload'] 并使用 xpath 检查器

进行了验证

以下是使用的 Webdriver 代码

WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//Test URL
driver.get("URL");

// UN & PWD
driver.findElement(By.xpath("//input[@id='UserName']")).clear();
driver.findElement(By.xpath("//input[@id='UserName']")).sendKeys("UN");
driver.findElement(By.xpath("//input[@id='Password']")).clear();        
driver.findElement(By.xpath("//input[@id='Password']")).sendKeys("pwd");
driver.findElement(By.xpath("//input[@id='btnSubmit']")).click();


//Validate the  User logged in 
if(driver.getPageSource().contains("UN Kumar"))
System.out.println("UN is logged in");
else
System.err.println("UN not logged in");



//Click on Asset Menu and LookUp Assets sub menu
Actions action = new Actions(driver);
 action.moveToElement(driver.findElement(By.xpath("//a[@href='http://ccqweb1.cloudapp.net//Main.aspx?    MenuId=15']"))).build().perform();
driver.findElement(By.linkText("Lookup Assets")).click();



//Search for Loan Number 
driver.findElement(By.xpath("//input[@id='ctl10_txtLoannumber']")).sendKeys("787878717");
driver.findElement(By.xpath("//input[@id='ctl10_cmdSearch']")).click();
Thread.sleep(2000);



// Get the window handle before clicking on link
String winHandleBefore = driver.getWindowHandle();

//Click on Initial Occupancy task
driver.findElement(By.xpath("//a[@id='ctl09_gvPendingTasks_ctl02_lnkTask']")).click();
Thread.sleep(2000);

// Switch to New Window 
String child = driver.getWindowHandle();
for(String parent : driver.getWindowHandles()){
    driver.switchTo().window(parent);
}


/**Exterior Photos tab **/

//Click on Exterior tab 
driver.findElement(By.xpath("//a[@id='ctl09_btnExteriorPhotos']")).click();


//Front View* `enter code here`

//Click  on Manual Upload button
driver.findElement(By.xpath("//div/input[@onclick='DisplayManualUpload(event)']")).click();

WebElement FileUpload_FrontView= driver.findElement(By.xpath("//input[@class='fleManuleUpload']"));
FileUpload_FrontView.sendKeys("\Front_view.jpeg");
Thread.sleep(500);

driver.findElement(By.xpath("//table/tbody/tr[1]/td[1]/div/div/div/input[@class='bttnUpload']")).click();

正在选择上传图片,但未点击上传按钮,因此无法上传文件。

你能帮我解决这个问题吗?

我很确定这只是一个错字。 以及上传定位器

(//input[@class='fleManuleUpload'] ->e.g. fleManualUpload )

至于上传按钮

([@class='bttnUpload'] -> btnUpload ).


总的来说: 为什么不使用更一致的 css 定位器或仅使用 ID?

By.xpath("//input[@id='UserName']" 

轻松使用:

By.id("UserName")


为了给你一个更好的答案,请在你运行这个时候上传错误信息。

这背后的原因是:

FileUpload_FrontView.sendKeys("C:\Users\prabhakar.y\Desktop\Pics for Uploading\Front_view.jpeg");

您的文件夹名称中有 space - "Pics for Uploading"。 试试给

File f = new File("Pics for Uploading\Front_view.jpeg");
FileUpload_FrontView.sendKeys(f.getAbsolutePath());

并且此文件夹应该在您当前的作品中space(这是一个很好的做法)。或者从文件夹名称中删除 space。