driver.findElement(By.xpath("//*[@id='printshapesEdit-main']/div/div[2]/div/div[2]/input")).sendKeys(Keys.chord(Keys.CONTROL, "v"));
For the above code, I would like to retrieve the copied text to a string. Please do me favor.
I can't really see why you would use copy/paste to get the text from an element and then enter it into a field and since you haven't provided a reason for it here's an easier way:
Get text from an element:
public String getElementText(By locator) {
WebElement locatorElement = driver.findElement(locator);
return locatorElement.getText();
}
Enter text to a field:
public void enterTextIntoField(String text, By locator) {
WebElement locatorElement = driver.findElement(locator);
locatorElement.click();
locatorElement.clear();
locatorElement.sendKeys(text);
}
please use below code
public void getClipboardContents()
throws UnsupportedFlavorException, IOException {
String result = "google.com";
System.setProperty("webdriver.chrome.driver", "E:\\New folder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/");
String copy = Keys.chord(Keys.CONTROL,Keys.chord("c"));
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("google.com");
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys(Keys.CONTROL+"a");
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys(copy);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
String x = (String) contents.getTransferData(DataFlavor.stringFlavor);
System.out.println(x);
int a= result.length();
int b = x.length();
System.out.println(a);
System.out.println(b);
if(a<=b)
{
System.out.println("Matched Character length")
}else
{
System.out.println("Issue In Character length");
}
}
}
In automation testing company we use below mentioned java code to read text from text file, and you can use this method directly where you want to enter text in filed.
public static String getTextFromTextFile() {
//FilePath
String sFilePath = "path to text file";
//Creating FileReader object
FileReader fr = null;
//Creating BufferedReader object
BufferedReader txtReader = null;
//Handling Exception using Try-Catch
String sCurrentLine = null;
try {
fr = new FileReader(sFilePath);
txtReader = new BufferedReader(fr);
//Reading file until file is null
while ((sCurrentLine = txtReader.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (txtReader != null)txtReader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return sCurrentLine;
}
and if you want to enter text in text field you can use selenium function sendkeys(). Below is the code:
public string enterTextInField (){
driver.findElement(By.Locator).clear();
driver.findElement(By.Locator).sendKeys(getTextFromTextFile());
}
Further you can use selenium functions getText() and sendkeys to get and enter text.
public string getTextOfElement(){
string elementText = driver.findElement(By.Locator).getText();
return elementText
}
public string enterTextInField (){
driver.findElement(By.Locator).clear();
driver.findElement(By.Locator).sendKeys(getTextOfElement());
}
For testing functionality of a COPY BUTTON or to verify whether the right string is copied or not we can check it by pasting the string in any input field and then retrieve the text from that field and compare it accordingly to verify the results.
Note:-- Since we cannot directly get the text into our local variable by any action we need a web element to get our text.
For Pasting the text you can use:--
Actions a = new Actions(driver); //to perform copy action
driver.findElement(By.id("inputBoxId")).clear(); //if its already having any text
driver.findElement(By.id("inputBoxId")).sendKeys(Keys.chord(Keys.CONTROL, "v")); //to perform paste operation in input box
driver.findElement(By.xpath("//input[@value='Save']")).click(); //save the value else the value attribute of input box will not change.
String getDataInLocalVar = driver.findElement(By.xpath("//*[@id='inputBoxId']")).getAttribute("value");
So basically, there is no straight way of doing this. Not sure why this was not thought out. A possible workaround is the following:
String copiedText = driver.findElement(By.xpath("//*[@id='printshapesEdit-main']/div/div[2]/div/div[2]/input")).getAttribute("value").toString();
System.out.println(copiedText);
Once you have issued a 'ctrl+v' to the input field, you can issue a .getAttribute()
to the same input field and move it into a variable.
Hope this helps. Cheers!
This will work on c#.
driver.FindElement(By.CssSelector("body")).SendKeys(OpenQA.Selenium.Keys.Control + "a");
driver.FindElement(By.CssSelector("body")).SendKeys(OpenQA.Selenium.Keys.Control + "c");
var txt = Clipboard.GetText();
I like this better then driver.FindElement(By.CssSelector("body")).Text
, because Text
returns all kinds of extra chars, this returns clean text. Works especialy well with tables returning tab delimted rows that can easily be pasted into excel.
getAttribute("value")
on the located WebElement to retrieve the current text value.