0

This element is present in a contact form page it is like an uploading url where we need to enter the url of the image and press enter

The code used by me is...with this code iam able to enter the text into that element but Enter key is not pressed

    driver.findElement(By.xpath("//*[@id='uploading-url']/input")).sendKeys("http://www.mytabletbooksqa.com/ProductImages/test1.gif");

     WebElement dropdownlists = driver.findElement(By.xpath("//*[@id='uploading-url']/input"));

     Actions builder=new Actions(driver);

     builder.clickAndHold(dropdownlists).sendKeys(dropdownlists, Keys.ENTER).release().build();

     builder.perform();

The HTML code for that element is

<div id="uploading-url">
<p class="reduce-space"><img alt="web_image" src="/images/content/duelr/web.png"></p>
<input placeholder="Enter URLs to upload from web" type="text"></div>   

Please guide me on what could possibly be going wrong here. Thank you for your time.

Vicky
  • 2,999
  • 2
  • 21
  • 36

3 Answers3

4

Following code can help:

WebElement dropdownlists = driver.findElement(By.xpath("//*[@id='uploading-url']/input"));
dropdownlists.sendKeys("http://www.mytabletbooksqa.com/ProductImages/test1.gif");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Lemme know if this works for you!

Sitam Jana
  • 3,123
  • 2
  • 21
  • 40
  • Sorry this is not working....when i run the code it throws me an error message "Exception in thread "main" java.lang.IllegalArgumentException: Key Down / Up events only make sense for modifier keys. at org.openqa.selenium.interactions.internal.SingleKeyAction.(SingleKeyAction.java:45) at org.openqa.selenium.interactions.KeyDownAction.(KeyDownAction.java:31) at org.openqa.selenium.interactions.Actions.keyDown(Actions.java:97)" – Vicky Aug 19 '14 at 09:43
  • Edited my previous response. Used **Robot** utility in Java. Please lemme know if this works for you! – Sitam Jana Aug 19 '14 at 10:10
  • Happy to be of help :) – Sitam Jana Aug 20 '14 at 11:30
  • This will for sure fail in headless mode. – Vinay Prajapati May 28 '18 at 11:15
0

It looks like you doing them all together. I could be a timing thing. I would recommend you separate the actions out a little bit.

 Actions builder=new Actions(driver);
 builder.clickAndHold(dropdownlists).perform();
 builder.sendKeys(Keys.ENTER).perform();
 builder.release().perform();

Then step through these and see what happens one at a time. If it works stepping through and not all at once then I would add a wait statement between the dropdown and the enter. I also removed the element part of the send keys as you already have the focus from the previous action.

mutt
  • 783
  • 1
  • 7
  • 11
  • sendkeys(keys.Enter)is not working ie) we are only able to enter the url into that element.Enter key is not getting pressed...kindly suggest me – Vicky Aug 19 '14 at 09:30
  • I'm really wondering why you are doing a clickAndHold instead of just a click. I would seem more normal to click, then type, and then hit enter and not have to actually hold the mouse down to click enter. Could you try it with just a click and then enter? – mutt Aug 19 '14 at 12:46
0

While using AWT might work on a browser it will fail on the headless mode.

You could use JDollarx library for same and just write the following to trigger a key press and release event.

sendKeys(Keys.ENTER).toBrowser();

https://github.com/loyada/dollarx

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86