nevergiveup
nevergiveup

Reputation: 63

instead of sendkeys last copied item from clipboard is getting entered in the element

In my application I have an email field and I'm using sendkeys to send an email address, but instead of sendkeys selenium inserts the value from clipboard. Do anyone faced this issue?

code:

WebElement email=driver.findElement(By.xpath("//div[@class='auth-container']//input[@type='text']");
email.sendKeys("[email protected]")

since Id is dynamic I have taken xpath from parent container

Upvotes: 3

Views: 1914

Answers (5)

Adrián
Adrián

Reputation: 1

This works for me:

chromeoptions.AddUserProfilePreference("settings.language.current_input_method", "US");

Upvotes: 0

AlenL
AlenL

Reputation: 448

I have encountered this, but it seemed to be intermittent. The code that was working well for months, suddenly doing the same thing (pasting from clipboard in place of every @ aka "at sign" ). I was able to make it go away (won't say "fix") by updating Chromedriver. I suspect that this was because Chrome has auto-updated, but the Chromedriver didn't. Can't be sure, because also had to reboot in the process. In any case, this is not "normal". Email addresses with @ sign should be passed through sendKeys without problems.

Upvotes: 0

In Node.js javascript, on hungarian keyboard, the @ sign in sendKeys has replaced with the content of clipboard. If you try to replace "@" width its unicode value "\u0040", the result is same, the sendKeys has has replaced with the content of clipboard, even though try to the tricky Key.chord( Key.ALT, "v", Key.NULL ) form, it does nothing.

// if clipboard contains XXX 
WebElement email=driver.findElement(By.xpath("//div[@class='auth-container']//input[@type='text']");
email.sendKeys("[email protected]")
// result: field content is sampleXXXsample.com

If you put @ sign on the clipboard, it works well, but you cant guarantee the content of clipboard in test automation. There is a way, to send keys to the active item.

I would avoid to fill the field with javascript, as mentioned above, because javascript fill it directly, does not care with the field status, fill it with value even though the field is inactive, do not triggers events, etc, javascript is brute force fill the field.

Send. Keys. There is a npm package for it. It is a wrapper for powershell sendkeys, so works only on windows.

npm install -g sendkeys

and the test code:

const sendkeys = require( 'sendkeys');
WebElement email=driver.findElement(By.xpath("//div[@class='auth-container']//input[@type='text']");
email.sendKeys("sample")
// result: cursor is in the field, field contains 'sample'
sendkeys.sync( "@sample.com" )
// result: field content is '[email protected]'

Upvotes: 2

Pradnya Bolli
Pradnya Bolli

Reputation: 1943

I am run script in java, hope this help you.. Using javascript I successfully send data into textbox.

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('identifierId').value='admin'");

Another way :-

WebElement Username = driver.findElement(By.xpath("//*[@id=\"identifierId\"]"));
((JavascriptExecutor)driver).executeAsyncScript("arguments[0].value='admin'",Username);

Here we are passing WebElement as a argument to the javaScript.

For more information refer this link

Another way is using robot class ..Common solution is to use the clipboard, Copy text and paste that text into text field.

String username = "admin";
StringSelection stringSelection = new StringSelection(username);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);

This doesn't type the entire "string" but helps to type whatever you want other than one character at a time.

String un="admin";
 char c;
 int l=un.length(),v1=0,v2=0;

 //System.out.println(l);
 while(v1<l)
{
 c=un.charAt(v1);
 v2=(int) c; //converts character to Unicode. 
 robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(v2));

   Thread.sleep(150);
   robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(v2));

   v1++;
}

see it works perfectly and it's awesome! Though it doesn't work for special characters which cannot be traced by unicode like |,!...etc.

Upvotes: 0

maestro.inc
maestro.inc

Reputation: 815

If send keys is not working try using action chains. It is often a good way to solve selenium interaction problems.

from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains(driver)
email = driver.find_element_by_xpath("//div[@class='auth-container']//input[@type='text']"))
action.send_keys_to_element(email, "[email protected]").perform()

hope this helps

Upvotes: 0

Related Questions