How to browse and upload file through selenium Web driver - selenium-webdriver

I am trying to upload a file through selenium webDriver but selenium gives an error:Unable to locate element. I have used all method to find element and click on browse but not any click occurring on that.
The HTML is <input type="file" onmousedown="this.blur();" onclick="//this.blur();" onchange="$('file-loader').show(); this.form.submit();" name="metadata_item" id="metadata_item">

First check the input element is visible
Then, you don't have to click on the browse button, it will trigger an OS level dialogue box and effectively stop your test dead.
In order to deal with this follow this code:
driver.findElement(By.id("myElementId")).sendKeys("<pathToFile>");
myElementId is the id of that element (button in this case) and in sendKeys you have to specify the absolute path of the content you want to upload. The Webdriver will do the rest for you.
Keep in mind that the upload will work only If the element you send a file should be in the form

cant answer without HTML code.
in order to click Browse button place the pointer in previous field i.e,
email address field and use
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
and then a system popup displays then use AutoIT tool to handle it

It could be under another frame.
Try switching to that frame and click the element.
It would work.
For Example - If the browse button is under another frame which has Id = "frameUpload", then switch the webdriver to that frame like this:
driver.switchTo().frame("frameUpload");
Now click on the browse button like this:
driver.findElement(By.Id("Id of the button")).click();
once you are done with the click, you can always get back to the default window like this
driver.switchTo().DefaultContent();

Related

click method is not working properly in selenium webdriver

i am trying to Automate flipkart website in which i am trying to change address but "add new adress" is not getting clicked i have attached the snapshot
my code is like driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).click();
please give the appropriate help
I doesn't look that you are clicking active element, the xpath is //*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span not correct it clicks on some span.
Use Firepath https://addons.mozilla.org/en-US/firefox/addon/firepath/ to get the xpath.
To ensure that button is clickable Use isDisplayed() and isEnabled() method before clicking on "Add New Address" button, this method return boolean value.
driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).isDisplayed();
driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).isEnabled();
Also you can verify that element is exist on page or not using below code
if(driver.findElements(byVal).size()!=0){
// Element is present.
}
hope it may helpful to identify cause of issue, why its not clickable.
First and foremost, Use a customized Xpath instead of the one you are using which is extracted directly from the browser. If no customized Xpath can be constructed then try a customized Css or any other locator if possible.
Try to go through the following attempts in order (I hope you can grasp why this is):
1- If .click() still doesn’t work, then keep on changing the values of the attributes you are using to construct your customized xpath, cssSelector, or locator.
2- Use the Actions class.
3- Use JavaScript executioner
4- instead of click(), try using any of: .sendKeys(“\n”). Or .sendKeys(keys.ENTER) or .sendKeys(keys.RETURN)

Not able to click and element using Selenium

I am trying to automate 'Tableau' login screen and download reports flow. Everything works fine till I reach the 'Download' button. The first problem is I am NOT able to right click on the Download button and do an 'Inspect' in Chrome which is weird. I somehow am able to find the xpath by going through the whole page. When i try to click it I get "Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div[2]/div/div[3]/div[4]/span[1]"}".
The code that I am using is mentioned below:
String xPath = "/html/body/div[2]/div[2]/div/div[3]/div[4]/span[1]";
(new WebDriverWait(driver, 20))
.until(ExpectedConditions.elementToBeClickable(By.xpath(xPath)));
driver.findElement(By.xpath(xPath)).click();
The page layout is like this from where I am picking the xpath
<div class="tabToolbarButton tab-widget" style="-webkit-user-select: none; -webkit-tap-highlight-color: transparent; width: 77.4px;">
<span class="tabToolbarButtonImg tab-icon-download"></span>
<span class="tabToolbarButtonText">Download</span></div>
<span class="tabToolbarButtonImg tab-icon-download"></span>
<span class="tabToolbarButtonText">Download</span>
</div>
Any help would be appreciated. What could be the reason for not able to 'Inspect'??
If you are not able to right click on the element, you can still right click anywhere on the page and select Inspect this element. You can then grab little binocular icon(tool tip message-select an element in the page to inspect it) and point it to the desired web element to check the xpath.
1st thing you need to check if you are writing xpath correctly. On a chrome you can do that without any plugin. On the developer window(same window which gets open when you select Inspect Element option), look for Console window and write your xpath as mentioned below and hit Enter.
$x("Write x path here")
for e.g. xpath for google search box
$x("//input[#id='lst-ib']")
I'm not sure why you writing absolute xpath. Its a bad practice. You can write x path for Download button in better way.
e.g.
(//span[#class='tabToolbarButtonText'])
I got the problem. So it seems the web page I was trying to automate has used frames. That is why even after providing the xpath the element was not getting clicked. I had to use 'switchTo.frame' in order to get the frame in focus and then I was able to click the element. Thanks for all the responses.
What I have still not understood is that why I was not able to inspect the element directly but that is a different question.

Upload file using arrow mark in webdriver

I have to upload an excel file in the application.For that, I need to click on one arrow mark (i.e;browse button)which will open up the popup for the user to choose the file from local drives.Below is the code displayed when I did inspect element on the arrow mark.
" ".
Also, the path which will display after choosing the file will be greyed out before user selects the file. This will be enabled only when the user chooses some file from the local.
I was trying below code but it did not work. I think send keys is wrong option as the text path is greyed out.
Help me on this.
WebElement El = driver.findElement(By.id("'uploadInp'"));
driver.findElement(By.id("uploadInp")).click();
El.sendKeys("D:\\NTN 4\\Demo\\BulkCart_Template.xlsx");
Arrow HTML code:-
<input id="file1" type="file" value="" style="background-color: red;opacity:0;filter:alpha(opacity=0);cursor: pointer; padding-top:3px;width:25px;" name="file">
You do not need to click on browser button or open any pop-up. sendkeys will going to dump your path in HTML DOM for you
So don't click on browser button while use the same button element to perform sendkeys like below:-
WebElement El = driver.findElement(By.id("'uploadInp'"));
El.sendKeys("D:\\NTN 4\\Demo\\BulkCart_Template.xlsx");
Now you need to click on element which is going to start your upload. As you didn't mention and it's unclear according to your question that which element going to upload your file. Just find that element and click on it normally.
Hope it will help you :)

How to get the current URL in Robot framework?

When the 'apply' link is clicked, it opens a new browser in Robot framework. How to get current url of that page? Here is the code:
Open Server
Set Browser Implicit Wait 60
Go To ${server}/jobs
Element Should Be Visible xpath=.//*[#id='txtjobsearch']
Input Text xpath=.//*[#id='txtjobsearch'] ${job Title search}
Element Should Contain xpath=(.//*[#class='clearfix tit-job']/div)[1] ${Job title}
Element Should Be Visible xpath=(.//*[#class='btn btn-sm btn-primary btnApply'])[1]
Click Element xpath=(.//*[#class='btn btn-sm btn-primary btnApply'])[1]
After this line, it opens a new window. How to get url of newly opened page and do actions like input text?
Set Browser Implicit Wait 20
Wait Until Page Contains Element xpath=.//*[#class='text-primary']
Try this:
${url}= Get Location
getting the current URL is easy using the Execute Javascript SeleniumLibrary keyword:
${url} = Execute Javascript return window.location.href;
Have you tried using the Select Window keyword?
http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Select%20Window
Click Link popup_link # opens new window
Select Window popupName
Title Should Be Popup Title
Select Window # Chooses the main window again
It seems like you wish to verify the url of the new window which should be easily possible with the following:
Select Window | url=https://google.com
Obviously you need to replace the above url with what you're expecting. Let us know how you get on.

Cannot find button element on form using Webdriver

I'm having trouble trying to click on on a button within a form.
I've tried xpath, cssselector, className, id, but still cannot find it.
Here's the HTML snippet for the button:
<input type="button" value="Continue" id="ni-reg-btn-register" class="btnNext ni-reg-btn-register">
I'm using WebDriver in Java
Getting this trace:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 30.09 seconds
Frequency: 100%
Browser: Firefox
URL: https://subscription.thetimes.co.uk/webjourney/webj_capturecustomerdetails
I've tried each of the following lines of code one by one (but no luck):
driver.findElement(By.className("btnNext ni-reg-btn-register")).click();
driver.findElement(By.cssSelector("buttons#ni-reg-btn-register.btnNext ni-reg-btn-register")).click();
List<WebElement> buttonlist= driver.findElements(By.className("btnNext ni-reg-btn-register"));
driver.findElement(By.id("ni-reg-btn-register")).click();
driver.findElement(By.xpath("//*[#id="ni-reg-btn-register"]")).click();
I was able to click the Continue Button using the below xpath.
//input[#id='ni-reg-btn-register']
I would suggest trying a WebDriverWait call to wait for the element in question to exist on the page prior to interacting with it. The Java documentation can be found here. It appears that while you are using the correct locator and your page is working, there is a timing issue where Selenium is trying to access the element prior to it being loaded.
EDIT: I was unable to locate an element on the provided page with the supplied id. I assume I am missing a step, but I did a search on the HTML for that page and found nothing.
I dont find the above button on the page link you provided ..
better chk the page link and try to give some wait statement after page load ...
implicit wait would be better or simpli Thread.sleep(2000);

Resources