I have a problem with one element on the web page. When I want somehow to interact with it(click on it, sendKeys or something) and every time I'm getting an error IDE view. In the browser I can click on this button and I locate it right browser view. All other elements on this website are working well. My task is to upload a file. In this case i want to make this -> filePathUploadButton.sendKeys("C:\\Users\\ASuvorkov\\Desktop\\testverlG_serenity.png") .
Manually it would be so:
User clicks on this button
Then user selects file in dialog
And the user clicks on "open file" to upload a file
Do you have any suggestions or experience with it, because I didn't meet such kind of things yet?
PS even checking if element.isEnabled(), element.isDisplayed() causes this error and programm breaks up.
Code snippet(JAVA):
#FindBy(id = "fileupload") private WebElement filePathUploadButton;
public void downloadCoverPicture() {
waitABit(LONG_WAIT);
element(mediaTypeDropDownButton).waitUntilClickable();
mediaTypeDropDownButton.click();
element(mediaTypeDropDownList).waitUntilClickable();
mediaTypeDropDownList.click();
waitABit(LONG_WAIT);
element(filePathUploadButton).waitUntilClickable();
filePathUploadButton.sendKeys("C:\\Users\\ASuvorkov\\Desktop\\testverlG_serenity.png");
waitABit(50000);
}
Error stacktrace:
org.openqa.selenium.ElementNotVisibleException: The following error occurred: Timed out after 10 seconds. Element not available
at pages.NewBookAddPage.downloadCoverPicture(NewBookAddPage.java:71)
at steps.NewBookManagementSteps.downloadsCoverPicture(NewBookManagementSteps.java:52)
at tests.NewBookManagementTest.addsNewBook(NewBookManagementTest.java:43)
We cannot use selenium to interact with open file dialog. We can use robot class or autoit for that uses.
Click the file upload button and use the below robot class file upload implementation code.
Robot robot = new Robot();
StringSelection selection = new StringSelection("Absolute path of the file");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection,null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.setAutoDelay(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Related
I have a question,
I want to upload a file in a popup window that is open in my web application.
I can not inspect the element of the popup window, window that is opened (F12 not response in this window)
I try this solutions that not worked
WebDriver deiver2 = getWebDriver();
Thread.sleep(8000);
Alert alert = deiver2.switchTo().alert();
alert.sendKeys("yyyyy");
the second attemp is:
WebDriver deiver2 = getWebDriver();
Thread.sleep(8000);
deiver2.switchTo()
.activeElement()
.sendKeys(
"yyyyy");
System.out.println("END");
In the first attempt it says that no alert exists.
In the second attempt it passed but I do not see it feel value in the text field (still blank),
How I can upload a file in selenium via popup. (and how to inspect the path field to locate the element in the new popup?)
this is the popup screen.
this is what solved it
deiver2.switchTo()
.activeElement();
System.out.println("Window title: "+ deiver2.getTitle());
deiver2.findElement(By.xpath("//input[#type='file']"))
.sendKeys(
"X:\\AutomationFiles\\yyyyy.pdf");
I am very new to Selenium WebDriver with Java. There is a Upload button on a job portal. When I click on that button windows explorer is displayed to choose the file. there are open and cancel buttons on this window. i want to select the cancel button. since it is a windows explorer i cannot inspect the cancel button. how do we write the code for cancelling the button. Thanks in advance.
driver.get("https://my.indeed.com/resume?from=gnav-homepage&co=US&hl=en_US");
driver.manage().window().maximize();
Thread.sleep(3000);
driver.findElement(
By.xpath("//[#id='container']/div/div/div[2]/div/div/div[2]/div/div[1]/div/div[1]/button")).click();
I do not have Java programming skills. I hope the below python code is easy to translate. In my opinion, combining windows action with selenium calls is often flaky.
There is a hidden element called 'upload resume button', you can change the attribute value to see it on the UI, use the send keys method on the element to upload the resume.
from selenium.webdriver import Remote, DesiredCapabilities
driver = Remote(desired_capabilities=DesiredCapabilities.CHROME)
driver.get('https://my.indeed.com/resume?from=gnav-homepage&co=US&hl=en_US')
driver.execute_script(
"document.getElementById('upload-resume-button').setAttribute('class', '')"
)
upload_your_resume = driver.find_element_by_id('upload-resume-button')
upload_your_resume.send_keys(r'C:\test\resume.docx')
The above code worked in my local.
Short answer is you cant, but if you want to upload a local file you can use send_keys on the input field, sending the file path.
Here is a python exemple:
driver.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")
you can use Robot class to simulate native keyboard and mouse actions to interact with windows based pop-ups.
The shortcut to close any opened window is: “Alt + Space +C” – Closes the focused window.
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_ALT);
rb.keyPress(KeyEvent.VK_SPACE);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_SPACE);
rb.keyRelease(KeyEvent.VK_ALT);
I am using selenium to navigate to a page and take screenshots using Internet Explorer. But the problem is the login is taken care by a Javascript alert box. Now Selenium has a facility where the focus can be brought to the alert box by using Alert element and I have managed to bring the focus and also enter some values in the user name text box.
The problem is Selenium does not switch focus to the password text box and enters the username and password in the same box. I tried Java AWT Robot to click on the tab key and it changes the focus, but Selenium does not recognized this and it continues entering the user name and password in the same box.
Below is my code:
Robot robot = new Robot();
driver.get("the url");
Alert alert = driver.switchTo().alert();
alert.sendKeys("username");
robot.keyPress(KeyEvent.VK_TAB);
alert.sendKeys("password");
alert.accept();
What am I missing here? Is my approach here correct or do I have to take a different route?
hi Madusudanan Try the code by commenting the another switch method.
Robot robot = new Robot();
Alert alert=dr.switchTo().alert();
dr.get("the url");
alert.sendKeys("username");
//dr.switchTo().alert();
robot.keyPress(KeyEvent.VK_TAB);
alert.sendKeys("password");
alert.accept();
Not a Java answer but since I found this question searching for a .net answer to this problem.
If you're using .NET you'll need to use SendKeys rather than Robot
using System.Windows.Forms;
_driver.SwitchTo().Alert().SendKeys("Username");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("password");
SendKeys.SendWait("{Enter}");
Hope this helps someone!
According to your question, after focusing on Password field Selenium WebDriver failed to enter/input/type it's corresponding value. You can do type the password value by using Robot class. The following is the whole code:
//First write a method to use StringSelection
public void setClipboardData(String string) {
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
Robot robot = new Robot();
driver.get("Your URL");
Alert alert = driver.switchTo().alert();
alert.sendKeys("username");
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
//call setClipboardData method declared above
setClipboardData("Your Password");
//Copy Paste by using Robot class
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
alert.accept();
I create code like that from any source and work after add: a.keyRelease(KeyEvent.VK_ADD);
// Initialize InternetExplorerDriver Instance.
WebDriver driver = new InternetExplorerDriver();
// Load sample calc test URL.
driver.get("your homepage testing");
//Code to handle Basic Browser Authentication in Selenium.
Alert aa = driver.switchTo().alert();
Robot a = new Robot();
aa.sendKeys("beyond"+"\\"+"DND");
a.keyPress(KeyEvent.VK_TAB);
a.keyRelease(KeyEvent.VK_TAB);
a.keyRelease(KeyEvent.VK_ADD);
setClipboardData("P#ssw0rd");
a.keyPress(KeyEvent.VK_CONTROL);
a.keyPress(KeyEvent.VK_V);
a.keyRelease(KeyEvent.VK_V);
a.keyRelease(KeyEvent.VK_CONTROL);
aa.accept(); private static void setClipboardData(String string)StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Using java.awt.Robot class comes with two main drawbacks.
Mac issue:
On a Mac OS, when we instantiate a Robot class, Java app launches in the background and takes away the focus (so some in the community were sending VK_META (Cmd key) and VK_TAB to get back to the alert). Since copy & paste shortcut keys are different for Mac, that needs to be also handled (using VK_META in place of VK_CONTROL).
Jenkins or remote runner issue:
Even if we accommodated the issue above, eventually when tests are run from Jenkins job, Robot instantiation becomes a problem again (Robotframework selenium execution through jenkins not working)
Fortunately, sendKeys can handle the tab key (as a scan code) and the code below worked for me.
Alert alert = driver.switchTo().alert();
alert.sendKeys("username" + Keys.TAB.toString() + "password");
alert.accept();
I am trying to automate some testing on a 3rd-party site using selenium rc and facing an issue with file chooser. Going down the issue i found that it was a browser native file chooser issue. I was able to handle that but now the problem is that when I click upload button manually file explorer window opens up and when i try to do that through selenium test case it does not open even if selenium is clicking that button
Another issue is selenium only clicks and is able to find that button if i move my mouse over that button. Here is the related code snippet:
public void testBox() throws Exception {
selenium.setTimeout("10000000000");
selenium.open("/files");
selenium.click("id=login_button_credentials");
selenium.waitForPageToLoad("150000");
while(!selenium.isElementPresent("id=upload_split_arrow"))
{
Thread.sleep(10);
}
selenium.click("id=upload_split_arrow");
while(!selenium.isElementPresent("id=upload_file1"))
{
Thread.sleep(10);
}
selenium.click("id=upload_file1");
Thread.sleep(10000000);
}
Can anyone suggest me some workaround for that?
Don't click on upload file button, you just directly type the file path like below.
selenium.type("id=upload_split_arrow","/home/test/Desktop/YourFile.txt");
selenium.click("id=upload_file1");
I hope this will work for you.
If the element is of type file, you can try using attachFile function.
I saw that lots of people have Problems uploading a file in a test Environment with Selenium WebDriver. I use the selenium WebDriver and java, and had the same problem. I finally have found a solution, so i will post it here hoping that it helps someone else.
When i need to upload a file in a test, i click with Webdriver in the button and wait for the window "Open" to pop. And then i copy the path to the file in the clipboard and then paste it in the "open" window and click "Enter". This is working because when the window "open" pops up, the focus is always in the "open" button.
You will need these classes and method:
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
public static void setClipboardData(String string) {
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
And that is what i do, just after opening the "open" window:
setClipboardData("C:\\path to file\\example.jpg");
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
And that´s it. It is working for me, i hope it works for some of you.
Actually, there is an in-built technique for this, too. It should work in all browsers and operating systems.
Selenium 2 (WebDriver) Java example:
// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.xpath("//input[#type='file']"));
fileInput.sendKeys("C:/path/to/file.jpg");
The idea is to directly send the absolute path to the file to an element which you would usually click at to get the modal window - that is <input type='file' /> element.
Thanks Alex,
Java Robot API helped me for uploading file. I was fedup with File Upload using WebDriver. Following is the code I used (Small modification to yours):
Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000);
Thanks Alex! I needed this since I could not get the sendKeys function to work when used via Play framework 2.1 (fluentlenium wrapper). I am testing over Firefox [17.0.7] for Mac and had to make a few mods to get it working. Below is an approximation of the final snippet I used.
val file = new File(PATH_TO_IPA)
val stringSelection: StringSelection = new StringSelection(file.getAbsolutePath)
Toolkit.getDefaultToolkit.getSystemClipboard().setContents(stringSelection, null)
val robot: Robot = new Robot()
// Cmd + Tab is needed since it launches a Java app and the browser looses focus
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_TAB)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_TAB)
robot.delay(500)
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_SHIFT)
robot.keyPress(KeyEvent.VK_G)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_SHIFT)
robot.keyRelease(KeyEvent.VK_G)
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_V)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_V)
robot.keyPress(KeyEvent.VK_ENTER)
robot.keyRelease(KeyEvent.VK_ENTER)
robot.delay(500)
robot.keyPress(KeyEvent.VK_ENTER)
robot.keyRelease(KeyEvent.VK_ENTER)
The switching of application on Mac is much better to do with AppleScript. AppleScript is integrated to system, so it will be always functional and does not depend on order of apps on Cmd+Tab. Your test/app will be less fragile.
https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html
You need only detect you are on mac and has name of the application.
Runtime runtime = Runtime.getRuntime();
String[] args = { "osascript", "-e", "tell app \"Chrome\" to activate" };
Process process = runtime.exec(args);