0

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 ?

1
  • Unless the image path is incorrect, there is no reason your code will fail. Checked at my end. It is working fine. It is, in fact, clicking on the Upload button. You just have to wait for some time to see that the image is uploaded atop under Photos. Also, try using relative xpath rather than absolute one, for cleaner approach. Commented Jan 15, 2015 at 18:09

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks thomas, I have changed xpath to ID's . Below is the HTML code , Its not a typo mistake .<div class="manualUploadContainer" style="text-align: center; margin-top: 10px;"> <input class="fleManuleUpload" type="file" style="width:160px;" multiple="multiple" accept="image/*"> <br> <input class="bttnUpload" type="button" onclick="ManualUpload(event)" value="Upload"> . I didn't get any error message.@mthomas
Then also you can try to improve your XPath without ids. Try: By.cssSelector(".bttnUpload") instead.
1

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.

3 Comments

.Thanks for your prompt response ..Apologies for late reply ..I changed according to your suggestion ..The problem am facing here is not wrt image/file selection .Am able to see image is getting selected but the "Upload" button is not clicked ..However on the same web page we have same kind of upload buttons which is not having unique attributes .So I traversed the HTML tree structure and found the unique attribute for "Upload" button but while running the test .Upload button is not clicked .So i kindly request you to suggest me where am going wrong . Is it a problem with UI design ?@Vivek
Is it? Am able to upload the image to your website. I ran your code on FF 31.0 using selenium- 2.44.0. You can see that i have uploaded an image to your login id. And remember dont share credentials in questions unless explicitly asked.
[email protected] problem is solved and working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.