Prabhakar Y
Prabhakar Y

Reputation: 87

File Upload button is not working using Selenium webdriver

Am trying to upload a jpeg image to my application . But on webpage we can upload other jpeg images as well in their respective section. Here as per the HTML, Upload button have the same attributes defined for other upload sections.

However ,When I inspect the xpath for Unique element for Upload button , I found the below xpath=//table/tbody/tr[1]/td[1]/div/div/div/input[@class='bttnUpload'] and verified using xpath checker

Below is Webdriver code used

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();

The Upload image is getting selected but Upload button not is clicked so am not able to upload the file.

Can you help me to solve this ?

Upvotes: 0

Views: 2541

Answers (2)

Vivek Singh
Vivek Singh

Reputation: 3649

The reason behind this is:

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

You are having space in foldername - "Pics for Uploading". Try out by giving

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

and this folder should be inside your current workspace (Its a good practice). Or else remove space from foldername.

Upvotes: 1

mthomas
mthomas

Reputation: 433

I'm pretty sure it's just a typo. As well as for the upload locator

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

as for the upload button

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


In General: Why don't you use more consistent css locators or just ids?

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

use easily:

By.id("UserName")


To give you a better answer please upload the error message when you run this.

Upvotes: 2

Related Questions