Selenium looping sendKeys - java

So I have gotten far enough to locate the textbox on facebook, and send a person a message. Now I want to see if I can create a loop to send multiple messages at once. My current problem is I have a for loop:
for (int i = 0; i < 20; i++) {
messageBox.sendKeys("My friend's name " + i + " ");
messageBox.sendKeys(Keys.ENTER);
}
However, I can only do this once. So It will print "My friend's name0" and then stop. Java says: element is not attached to the page document.
I am assuming that because i sent the message, some part of the Inspect Element has changed? Is there a way to solve this?

Get Friend's name from Excel sheet
Make a Method for get data from excel file:-
public static void fn_FetchExcelData(String FilePath,String ColumnName) throws IOException{
String WbookPath=FilePath;
Workbook WBookObj=fn_GetWorkbook(FilePath);
Sheet SheetObj=WBookObj.getSheetAt(0);
Row FstRowObj=SheetObj.getRow(0);
int cellCount=FstRowObj.getLastCellNum();
int columnNumber=0;
ArrayList<String> AL=new ArrayList<String>();
for(int j=0; j<cellCount;j++){
if(FstRowObj.getCell(j, Row.CREATE_NULL_AS_BLANK).getStringCellValue().trim().equalsIgnoreCase(ColumnName)){
columnNumber=j;
break;
}
}
int rowcnt=SheetObj.getLastRowNum();
for(int i=1;i<=rowcnt;i++){
Cell fstcellObj=SheetObj.getRow(i).getCell(columnNumber, Row.CREATE_NULL_AS_BLANK);
String ColumnVal=fstcellObj.getStringCellValue();
AL.add(ColumnVal);
call excel method & use it
fn_FetchExcelData("Your excel data path");
int j;
int size = AL.size();
for(j=0;j<size;j++)
{
System.out.println(AL.get(j));
messageBox.sendKeys(AL.get(j));

Nevermind i figured it out, I just added
WebElement messageBox = driver.findElement(By.xpath("//textarea[#class='uiTextareaAutogrow _552m']"));
wait.until(ExpectedConditions.visibilityOf(messageBox));
Again to the loop and it works. However it is VERY slow.

Related

Below code is not woking with Chrome browser

I have created a reusable function which clicks the check box of a particular row and returns the text of that row.
CheckBoxXpath-> private static final String XPATH_JOBRATECATEGORIES_CHECKBOX_LIST = "//kendo-grid-list//table/tbody/tr/td[1]/label";
RowXpath -> private static final String XPATH_JOBRATECATEGORIES_LIST = "//kendo-grid-list//table/tbody/tr/td[2]//div//div";
count-> 0 (I want to click only first row check box)
public String Select_CheckBox_Return_SelectedText(String CheckBoxXpath,String RowXpath, int Count) {
List<WebElementFacade> listOfCheckBox = findAll(By.xpath(CheckBoxXpath));
List<WebElementFacade> listOfrow = findAll(By.xpath(RowXpath));
if(listOfCheckBox.size()>Count) {
for (int i = 0; i <= Count; i++) {
listOfCheckBox.get(i).click();
String Actual=listOfrow.get(i).getText();
}
}else {
Assert.fail("Need to have more rows to fullfill the requirement");
return null;
}
return Actual;
}
This is working fine with Firefox browser, but not working with Chrome browser.
On debugging code is throwing exception on -> "listOfCheckBox.get(i).click();"
I am not able to understand why it is behaving so weired.
Need help. Thanks in advance.
You need to target the checkboxes, not the labels in your xpath:
//kendo-grid-list//table/tbody/tr/td[1]/input[#type='checkbox']

Trim sheet name in Sheet interface (Apache POI)

I'm trying to get Sheet by using:
Sheet sheet=workbook.getSheet("sheetName");
But in my workbook, some sheet names has whitespaces. How to make "trim search"? Is is possible to do something like this:
Sheet sheet=workbook.trimSheetSearch("sheetName");
Or
Sheet sheet=workbook.indexOfSheetSearch("sheetName");
You should be able to iterate each Sheet to improve your search with Workbook.getNumberOfSheets() and Workbook.getSheetAt(int index).
Something like :
for(int i = 0; i < workbook.getNumberOfSheets(); i++){
String s = workbook.getSheetName(i);
if(checkName(s, nameToFind)) return workbook.getSheetAt(i);
}
return null; // sheet not found
For the search method, you can do a dirty solution like
public boolean checkName(String n1, String n2){
return n1.replaceAll(" ", "").equalsIgnoreCase(n2.replaceAll(" ", ""));
}
I let you improve that method (I will not do everything here ;) ). Note that this might not compile because I don't have an actual system to run this but this should work just fine.

Retrieve data from a field using selenium

I am a begineer in Automation Testing(using Selenium, JavaLang N Eclipse).
I am trying to fetch few values from a webpage and store them in an excel.
The input values are taken from excel and the output will be stored in an excel.
The problem currently i am facing is, let me tell you with an example.
Firstly, it takes account number say 123 from excel and inputs in to search field and then selects the appropriate account and fetches name and email.
Secondly the next account number say 234 is fetched from excel and inputs in to search field and then it gets the appropriate name and email of that account(234).
I am trying with just two accounts as if now.
So when i see the ouput or rather opens the excel file i only get the last account owner name and number in both the columns.
The result i needed is First account name and email in one column and second account name and email in another column.
I request all the folks to help me out with this!!.
Thanks for looking in to this..
int rc= ws.getRows();
Below is the code:
WebElement s1=driver.findElement(By.xpath("//table[#id='kInfo']/tbody/tr[10]/td"));
//WebElement s1=driver.findElement(By.xpath("//td[#class='tRight']/a"));
String s2=s1.getText();
System.out.println("data:"+s2);
String D1= driver.findElement(By.xpath("//div[#class='vcard clearfix']")).getText();
System.out.println(D1);
Thread.sleep(1000);
/*driver.findElement(By.xpath("//*[#id='homePage']")).click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Thread.sleep(1000);*/
WritableWorkbook wb1 = Workbook.createWorkbook( new File("C:\\Bhasker-DNT\\Excel\\ouput.xls"));
WritableSheet ws1 = wb1.createSheet("customsheet", 1);
{
for(int count=0;count<4;count++)
{
for(int i=0;i<rc-1;i++)
{
Label label = new Label(i, 0, s2);
ws1.addCell(label);
Label label1 = new Label(i, 1, D1);
ws1.addCell(label1);
}
}
//Label label = new Label(0, 0, s2);
//ws1.addCell(label);
}
wb1.write();
wb1.close();
To fetch element for a div or a selection or a p you need the xpath i suppose you have it.
Than
String name = ""
List<WebElement> your_list= driver.findElements(By.xpath("xpath_stuff or workbook.get(I)"));
for (int i=0; i<your_list.size(); i++) {
// System.out.println(i+ ". " + your_list.get(i).getText());
// here you can write the elements to excel.
//if you need first name you need to just
if(i==(you know which is the first row that conatins the name)
{
name = your_list.get(i).getText();
}
... the same way the email and the other
ws1.addCell(your_list.get(i).getText());
}
wb1.close();
I hope it helps, if not please provide us some example from the html and the from excel.

sheet.getLastRowNum() function gives invalid count with Apache POI 3.9 [duplicate]

This question already has answers here:
How to get row count in an Excel file using POI library?
(7 answers)
Closed 6 years ago.
I am using Apache POI 3.9 for xls and xlsx file processing.
As per the logic, I want to iterate over each row and want to collect data for further processing. For that I am using sheet.getLastRowNum() to retrieve number of rows in the xls sheet.
But it seems that sheet.getLastRowNum() gives wrong count if number of records are more than 10.
It works fine if total number of rows are 10. Otherwise it gives result deducted by one.
i.e If there are 15 row in the sheet then It gives 14 as Last Row number.
Can Anyone suggest me,How to solve this problem ??
Here is the code that i am using to collect the data ...
public static List<LinkedList<String>> populateExcelSheetContentWithHeader(Sheet sheet,int columnsCount,int rowsCount) throws Exception{
Row row = null;
List<LinkedList<String>> excelFileRecordsList = new LinkedList<LinkedList<String>>();
int emptyColCount = 0;
String freeTextData = null;
LinkedList<String> excelFileDataList =null;
int actualRows = sheet.getLastRowNum();
if (actualRows < rowsCount) {
rowsCount = actualRows;
}
///ITERATE OVER EACH ROW AND POPULATE THE DATA
for(int index=0;index<rowsCount;index++){
row = sheet.getRow(index);
if(row!=null){
emptyColCount = 0;
excelFileDataList =new LinkedList<String>();
for(int colIndex=0;colIndex<columnsCount;colIndex++){
freeTextData = "";
if(isEmptyCell(row.getCell(colIndex))){
emptyColCount++;
}else{
freeTextData = getCellValue(row.getCell(colIndex),false);
}
//ADD TEXT DETILS TO THE LIST
excelFileDataList.add(freeTextData);
}
//CHECK FOR END OF FILE
if(emptyColCount != columnsCount){
excelFileRecordsList.add(excelFileDataList);
}else{
break;
}
}
}
return excelFileRecordsList;
}
Any suggestion is appreciated .
it is possible that you are converting from CSV or you did any kind of modifications (copy or paste) inside your xls? I had the same exact behaviour when I did some changes like I mentioned. To fix it I had to copy the original again. Sorry for not providing a real solution, I just say what happened.
Maybe that helped

JOpenDocument reading from .ods file

I'm trying to read a .ods file from jopendocument. But when I am trying to access/print the value of a particular cell a blank output is displayed.My code is :
for(int nRowIndex = 0; nRowIndex < r; nRowIndex++)
{
int nColIndex = 0;
for( ;nColIndex < t; nColIndex++)
{
System.out.println(sheet.getCellAt(nColIndex, nRowIndex).toString());
}
}
This gets printed on console:
<table:table-cell xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" />
Any help will be appreciated.
The method getCellAt returns an Object of type Cell, to get the contained value as text use
sheet.getCellAt(nColIndex, nRowIndex).getTextValue()

Categories