I want to write script for dynamically filling the contact forms with different websites .
I tried This code ..but its very slow and also it's stopping, because By id or By name its not found
In a different websites with different fields by naming also different.. so how to solve this problem , could you please suggest me any other methods.
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import com.google.common.collect.Table.Cell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TestClass {
@Test
public void Fillingform() throws InterruptedException
{
ArrayList<String> excellinks=new ArrayList<String>();
System.out.println("im here");
try {
FileInputStream file = new FileInputStream(new File("C:\\Users\\Varalakshmi.CWSERVER\\Desktop\\documents\\Book1.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<org.apache.poi.ss.usermodel.Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
org.apache.poi.ss.usermodel.Cell cell = cellIterator.next();
excellinks.add(cell.getStringCellValue());
}
System.out.println("");
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
for(Iterator<String> i = excellinks.iterator(); i.hasNext(); ) {
String item = i.next();
System.out.println("Launching chrome browser");
System.setProperty("webdriver.chrome.driver", "D://chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get(item);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
By Name = By.name("name");
By Name1 = By.name("CF_NAME_FIRST");
By Name2 = By.name("contact_name");
By Name3 = By.name("your-name");
By Name4 = By.name("submitted[name]");
By Name5 = By.name("firstname");
By Name6 = By.name("text-875");
By Name7 = By.name("Name");
By Name8 = By.name("patient_name");
By Name9 = By.name("first_name");
By Name10 = By.name("First-Name");
By Name11 = By.name("first_name_");
if(driver.findElements(By.name("name")).size() > 0){
driver.findElement(Name).sendKeys("ChronicWatch");
}
else if (driver.findElements(Name1).size() > 0){
driver.findElement(Name1).sendKeys("Chronic");
driver.findElement(By.name("CF_NAME_LAST")).sendKeys("Watch");
}
else if (driver.findElements(Name2).size() > 0){
driver.findElement(Name2).sendKeys("ChronicWatch");
}
else if (driver.findElements(Name3).size() > 0){
driver.findElement(Name3).sendKeys("ChronicWatch");
}
else if (driver.findElements(Name4).size() > 0){
driver.findElement(Name4).sendKeys("ChronicWatch");
}
else if (driver.findElements(Name5).size() > 0){
driver.findElement(Name5).sendKeys("Chronic");
driver.findElement(By.name("lastname")).sendKeys("Watch");
}
else if (driver.findElements(Name6).size() > 0){
driver.findElement(Name6).sendKeys("ChronicWatch");
}
else if (driver.findElements(Name7).size() > 0){
driver.findElement(Name7).sendKeys("ChronicWatch");
}
else if (driver.findElements(Name8).size() > 0){
driver.findElement(Name8).sendKeys("ChronicWatch");
}
else if (driver.findElements(Name9).size() > 0){
driver.findElement(Name9).sendKeys("Chronic");
driver.findElement(By.name("last_name")).sendKeys("Watch");
}
else if (driver.findElements(Name10).size() > 0){
driver.findElement(Name10).sendKeys("Chronic");
driver.findElement(By.name("Last-Name")).sendKeys("Watch");
}
else if (driver.findElements(Name11).size() > 0){
driver.findElement(Name11).sendKeys("Chronic");
driver.findElement(By.name("first_name_1")).sendKeys("Watch");
}
By Phone = By.name("phone");
By Phone1 = By.name("CF_PHONE");
By Phone2 = By.name("contact_phone");
By Phone3 = By.id("phone");
By Phone4 = By.name("number");
By Phone5 = By.name("your-phone");
By Phone6 = By.name("phone_number");
By Phone7 = By.name("Phone");
//System.out.println(driver.findElements(Phone).size() );
if(driver.findElements(By.name("phone")).size() > 0){
}
else if (driver.findElements(Phone1).size() > 0){
}
else if (driver.findElements(Phone2).size() > 0){
}
else if (driver.findElements(Phone3).size() > 0){
}
else if (driver.findElements(Phone4).size() > 0){
}
else if (driver.findElements(Phone5).size() > 0){
}
else if (driver.findElements(Phone6).size() > 0){
}
else if (driver.findElements(Phone7).size() > 0){
}
By Alternatephone = By.name("CF_ALTERNATE_PHONE");
if(driver.findElements(Alternatephone).size() > 0){
driver.findElement(Alternatephone).sendKeys("
866-690-3995");
}
else {
System.out.println("No alternative phone number");
}
By Email = By.name("email");
By Email1 = By.name("CF_EMAIL");
By Email2 = By.name("contact_email");
By Email3 = By.name("your-email");
By Email4 = By.name("submitted[email_address]");
By Email5 = By.id("email");
By Email6 = By.name("Email");
if(driver.findElements(Email).size() > 0){
}
else if (driver.findElements(Email1).size() > 0){
}
else if (driver.findElements(Email2).size() > 0){
}
else if (driver.findElements(Email3).size() > 0){
}
else if (driver.findElements(Email4).size() > 0){
}
else if (driver.findElements(Email5).size() > 0){
}
else if (driver.findElements(Email6).size() > 0){
}
By DOB = By.name("birth-date");
if(driver.findElements(DOB).size() > 0){
driver.findElement(DOB).sendKeys("01/01/1970");
}
else {
System.out.println("No date of birth");
}
By Subject = By.name("your-subject");
By Subject1 = By.id("subject");
By Subject2 = By.name("Subject");
if(driver.findElements(Subject).size() > 0){
driver.findElement(Subject).sendKeys("Care Management");
}
else if (driver.findElements(Subject1).size() > 0){
driver.findElement(Subject1).sendKeys("Care Management");
} else if (driver.findElements(Subject2).size() > 0){
driver.findElement(Subject2).sendKeys("Care Management");
}
By locator = By.name("question");
By locator1 = By.name("message");
By locator2 = By.name("contact_message");
By locator3 = By.name("CF_DESCRIBE");
By locator4 = By.name("comments");
By locator5 = By.name("your-message");
if(driver.findElements(locator).size() > 0){
driver.findElement(locator).sendKeys("Prevention is better than cure! Want to help keep Chronically ill Medicare patients healthy? and help physicians to add additional revenues? Read below, Medicare has started to reimburse Physicians to keep Chronically ill patients healthy and in their home. Reimbursement Code is 99490. ");
driver.findElement(locator). sendKeys("ChronicWatch can help providers increase their revenue potential from CCM with minimal implementation and this can easily be incorporated into a provider's daily workflow with a very clean solution for data exchange from and to electronic health records (EHR). Our solution also offers features that allow families and caregivers to indicate if there is evidence of worsening in a patient's condition. Want to know more? Call
866-690-3995");
}
else if (driver.findElements(locator1).size() > 0){
driver.findElement(locator1).sendKeys("Prevention is better than cure! Want to help keep Chronically ill Medicare patients healthy? and help physicians to add additional revenues? Read below, Medicare has started to reimburse Physicians to keep Chronically ill patients healthy and in their home. Reimbursement Code is 99490. ");
driver.findElement(locator1). sendKeys("ChronicWatch can help providers increase their revenue potential from CCM with minimal implementation and this can easily be incorporated into a provider's daily workflow with a very clean solution for data exchange from and to electronic health records (EHR). Our solution also offers features that allow families and caregivers to indicate if there is evidence of worsening in a patient's condition. Want to know more? Call
866-690-3995");
}
else if (driver.findElements(locator2).size() > 0){
driver.findElement(locator2).sendKeys("Prevention is better than cure! Want to help keep Chronically ill Medicare patients healthy? and help physicians to add additional revenues? Read below, Medicare has started to reimburse Physicians to keep Chronically ill patients healthy and in their home. Reimbursement Code is 99490. ");
driver.findElement(locator2). sendKeys("ChronicWatch can help providers increase their revenue potential from CCM with minimal implementation and this can easily be incorporated into a provider's daily workflow with a very clean solution for data exchange from and to electronic health records (EHR). Our solution also offers features that allow families and caregivers to indicate if there is evidence of worsening in a patient's condition. Want to know more? Call
866-690-3995");
}
else if (driver.findElements(locator3).size() > 0){
driver.findElement(locator3).sendKeys("Prevention is better than cure! Want to help keep Chronically ill Medicare patients healthy? and help physicians to add additional revenues? Read below, Medicare has started to reimburse Physicians to keep Chronically ill patients healthy and in their home. Reimbursement Code is 99490. ");
driver.findElement(locator3). sendKeys("ChronicWatch can help providers increase their revenue potential from CCM with minimal implementation and this can easily be incorporated into a provider's daily workflow with a very clean solution for data exchange from and to electronic health records (EHR). Our solution also offers features that allow families and caregivers to indicate if there is evidence of worsening in a patient's condition. Want to know more? Call
866-690-3995");
}
else if (driver.findElements(locator4).size() > 0){
driver.findElement(locator4).sendKeys("Prevention is better than cure! Want to help keep Chronically ill Medicare patients healthy? and help physicians to add additional revenues? Read below, Medicare has started to reimburse Physicians to keep Chronically ill patients healthy and in their home. Reimbursement Code is 99490. ");
driver.findElement(locator4). sendKeys("ChronicWatch can help providers increase their revenue potential from CCM with minimal implementation and this can easily be incorporated into a provider's daily workflow with a very clean solution for data exchange from and to electronic health records (EHR). Our solution also offers features that allow families and caregivers to indicate if there is evidence of worsening in a patient's condition. Want to know more? Call
866-690-3995");
}
else if (driver.findElements(locator5).size() > 0){
driver.findElement(locator5).sendKeys("Prevention is better than cure! Want to help keep Chronically ill Medicare patients healthy? and help physicians to add additional revenues? Read below, Medicare has started to reimburse Physicians to keep Chronically ill patients healthy and in their home. Reimbursement Code is 99490. ");
driver.findElement(locator5). sendKeys("ChronicWatch can help providers increase their revenue potential from CCM with minimal implementation and this can easily be incorporated into a provider's daily workflow with a very clean solution for data exchange from and to electronic health records (EHR). Our solution also offers features that allow families and caregivers to indicate if there is evidence of worsening in a patient's condition. Want to know more? Call
866-690-3995");
}
By Submit5 = By.cssSelector("input[type='submit']");
By Submit1 = By.name("Submit Registration");
By Submit2 = By.name("blogin");
By Submit3 = By.linkText("Submit");
By Submit4 = By.linkText("Send");
By Submit6 = By.linkText("Send Feedback");
if(driver.findElements(Submit5).size() > 0){
driver.findElement(Submit5).click();
}
else if (driver.findElements(Submit1).size() > 0){
driver.findElement(Submit1).click();
}
else if (driver.findElements(Submit2).size() > 0){
driver.findElement(Submit2).click();
}
else if (driver.findElements(Submit3).size() > 0){
driver.findElement(Submit3).click();
}
else if (driver.findElements(Submit4).size() > 0){
driver.findElement(Submit4).click();
}
else if (driver.findElements(Submit6).size() > 0){
driver.findElement(Submit6).click();
}
Thread.sleep(2000);
}
} }