How to upload file using selenium webdriver for invisible element - file-upload

I am unable to upload file using webdriver can you please help me.
I need to upload cover letter and resume my webpage. .
I have tried with code:
myTestDriver.findElement(By.id("uploadcover")).sendKeys("C:\\Users\\hadmin\\Desktop\\test.doc");
myTestDriver.findElement(By.id("uploadresumeimg")).sendKeys("C:\\Users\\hadmin\\Desktop\\test.doc");
But it is not working, Please give me solution.

Make invisible element visible:
String js = "arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1;";
((JavascriptExecutor) driver).executeScript(js, elem);
from: selenium faq Q:Does WebDriver support file uploads?

Related

Amazon click image based on search

I am very new to selenium and tried this and it didn't work. You can easily reproduce this by going to amazon site and search for hairclip and you will find this image in the search. Once this image is found, i want to go to next page but it is not happening.
System.setProperty("webdriver.chrome.driver", "C:\\software\\chromedriver_win32\\chromedriver.exe");
ChromeDriver Driver = new ChromeDriver();
Driver.get("http://www.amazon.com");
Driver.manage().window().maximize();
Driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Hairclip");
Driver.findElementById("nav-search-submit-button").click();
By by = By.xpath("//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')]");
WebDriverWait w = new WebDriverWait(Driver, 20);
WebElement element = w.until(ExpectedConditions.elementToBeClickable(by));
element.click();
Error as below
Expected condition failed: waiting for element to be clickable:
By.xpath:
//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL.AC_UL320.jpg')]
(tried for 20 second(s) with 500 milliseconds interval)
I appreciate your time for the reply and effort.
Looks like you wanna click on first image, in that case this xpath should work :
//img[#data-image-index='1']
try it like :
WebElement element = w.until(ExpectedConditions.elementToBeClickable(by.xpath("//img[#data-image-index='1']")));
element.click();
The XPath locator you are using //img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')] matches 4 elements on that page.
To click the element you want please use this locator:
(//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')])[last()]
So the code line will be
By by = By.xpath("(//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')])[last()]");

File upload using selenium Web driver?

WebElement product_image = driver.findElement(By.id("pdt_upload_preview"));
System.out.println("-------Product image selected----------\n-----------------------");
product_image.click();
product_image.sendKeys("C:\\TEST\\TEST\\Images\\Images(1).jpg");
I can't upload the file by using this.
I hope no need to click, simply try
WebElement product_image = driver.findElement(By.id("pdt_upload_preview"));
product_image.sendKeys("C:\\TEST\\TEST\\Images\\Images(1).jpg");
If your code isn't working, it's possible that your image upload element is hidden. You could try executing Javascript to reveal the element:
WebElement product_image = driver.findElement(By.id("pdt_upload_preview"));
// execute javascript to reveal the element
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.display = 'block';", product_image);
// send keys to the element
product_image.sendKeys("C:\\TEST\\TEST\\Images\\Images(1).jpg");

How to disable mouse right click in selenium in java

I have written an application in selenium using JAVA to login into website. Following is my code
import org.openqa.selenium.JavascriptExecutor;
WebDriver driver = new FirefoxDriver();
driver.get("http://inernalportal.com");
WebElement element = null;
element = driver.findElement(By.id("txtLoginID"));
element.sendKeys("user");
element = driver.findElement(By.id("txtpassID"));
element.sendKeys("password");
element = driver.findElement(By.id("btnLogin"));
element.click();
However above code works correctly but there is one issue, while filling password by Seleinum driver user could click in address bar to see password.
Is there any way to prevent left/right mouse click in Selenium?
Your help is highly appreciable.
That should resolve problem :)
((JavascriptExecutor) driver).executeScript("document.getElementById(\"txtpassID\").value = \"password\";");

KeyPress Enter for Selenium

We are doing automation testing and came around with a situation where i need to download the file from the browser .
In Download when the download button is hit we are coming to the system pop for the download where we need to perform the enter operation .
Can some one help us how to perform the enter or keyboard operation currently we are using robot API but it is not working on grid system ,
Here is my code for robot can it be enhanced and used or do we have any alternate way to do it
******** Code *************
public void downloadReportFromMyExport(WebDriver driver, String downloadSufixId) throws AWTException,
InterruptedException
{
String downloadPrefixId = ConfigProperty.getConfig("downloadPrefixId").trim();
String[] suffix;
suffix = StringUtil.split(downloadSufixId, "TR_EXP_");
String suffixPart = suffix[0];
String completeId = downloadPrefixId.concat(suffixPart);
By id = By.id(completeId);
WebElement element = driver.findElement(id);
element.click();
Robot pressKey = new Robot();
pressKey.keyPress(KeyEvent.VK_ENTER);
pressKey.keyRelease(KeyEvent.VK_ENTER);
threadSleep("5");
pressKey.keyPress(KeyEvent.VK_ALT);
pressKey.keyPress(KeyEvent.VK_F4);
pressKey.keyRelease(KeyEvent.VK_F4);
pressKey.keyRelease(KeyEvent.VK_ALT);
logger.info("Downlaod Complete");
}
In firefox browser,
Solution-1
You can change the browser settings so that it saves all downloads to that location without asking.
Refer below link to know to change that setting in firefox.
https://support.mozilla.org/en-US/kb/startup-home-page-download-settings
Solution-2
By using firefox profile setting you can achieve this.
FirefoxProfile profile=new FirefoxProfile();
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.dir","C:\\Users\\Downloads\\"); profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver=new FirefoxDriver(dc);
yeah i've encountered the same issue
better change the browser settings to save in a particular path
for handling different browsers like,
in FF,
i've used
in firefox by default the control will be on "OPEN" option so..
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
for IE (to save alt+s,to open alt+O) here im saving the file
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
for chrome
by default,when ever you click dowload button it will save without showing any popups
and i've succeeded hope it helps you
-Ajay

How to verify if the image in Selenium using Webdriver

How would I verify the image is displaying is the correct path/name in Selenium using WebDriver?
I started using this code but not sure :
string _active = "<img style="display: ;" alt="Active" src="../App_Themes/Default/images/check.png"/>";
driver.FindElement(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_AddeCardControl1_gv']/tbody/tr[11]/td[7]/img")).Text.Contains(_active);
I would like to clarify to you that this code
driver.FindElement(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_AddeCardControl1_gv']/tbody/tr[11]/td[7]/img")).Text.Contains(_active);
does not give you the html code for the image tag but a IWebElement object. And you can read the various attributes of this WebElement by using the GetAttribute method.
You will have to get the src attribute of the img tag you are looking for by locating the image(webelement) by xpath and then
IWebElement element = driver.FindElement(By.XPath("Your xpath"));
string path = element.GetAttribute("src");
Now you can verify the path of your image. Hope this helps you.