java - How can I automate a large number of dropdowns without having to make webelement of each one seperately -


so,i have been asked automate website of sudden @ company , stuck on part have automate 5-6 dropdowns 1 after other

the code 5-6 webelements every dropdown doesnt seem right

json file::          {          "username": yawningemu3,          "password": any,          "cpassword": any,          "email": vishek3242@gmail.com,          "cemail": vishek3242@gmail.com,          "gender": man,          "country": india,          "ethnicity": indian          } 

i cant seem find way optimize given part of code:

      package com.thinksys.pof;        import java.io.filereader;       import java.util.list;        import org.openqa.selenium.by;       import org.openqa.selenium.webdriver;       import org.openqa.selenium.webelement;       import org.openqa.selenium.firefox.firefoxdriver;       import org.openqa.selenium.support.ui.expectedconditions;       import org.openqa.selenium.support.ui.select;       import org.openqa.selenium.support.ui.webdriverwait;        import com.google.gson.jsonobject;       import com.google.gson.jsonparser;         public class registration        {      public static void main(string... args) throws interruptedexception        {         {     jsonparser parser = new jsonparser();       try       {     object obj = parser.parse(new filereader("..//pof//data.json"));     jsonobject jsonobject = (jsonobject) obj;      string username = (string) jsonobject.get("username").getasstring();    string password = (string) jsonobject.get("password").getasstring();    string cpassword = (string) jsonobject.get("cpassword").getasstring();    string email = (string) jsonobject.get("email").getasstring();    string cemail = (string) jsonobject.get("cemail").getasstring();    string gender = (string) jsonobject.get("gender").getasstring();    string country = (string) jsonobject.get("country").getasstring();    string ethnicity = (string) jsonobject.get("ethnicity").getasstring();       system.setproperty("webdriver.gecko.driver","..//pof//geckodriver.exe");     webdriver driver = new firefoxdriver();      driver.get("https://www.pof.com/");      webdriverwait wait = new webdriverwait(driver, 60, 50);      wait.until(expectedconditions.visibilityofelementlocated(by.xpath(".//*           [@id='wrapper']/div[1]/div/div/span[1]/a"))).click();      driver.findelement(by.xpath(".//*[@id='blue-box-fluid-      round']/center/table/tbody/tr[1]/td[2]/input[3]")).sendkeys(username);      driver.findelement(by.xpath(".//*[@id='blue-box-fluid-       round']/center/table/tbody/tr[2]/td[2]/input")).sendkeys(password);      driver.findelement(by.xpath(".//*[@id='blue-box-fluid-       round']/center/table/tbody/tr[3]/td[2]/input")).sendkeys(cpassword);      driver.findelement(by.xpath(".//*[@id='blue-box-fluid-      round']/center/table/tbody/tr[4]/td[2]/input")).sendkeys(email);      driver.findelement(by.xpath(".//*[@id='blue-box-fluid-        round']/center/table/tbody/tr[5]/td[2]/input")).sendkeys(cemail);      webelement gen = driver.findelement(by.xpath(".//*[@id='blue-box-fluid-         round']/center/table/tbody/tr[6]/td[2]/select"));      thread.sleep(3000);      select index = new select(gen);      index.selectbyvisibletext(gender);      webelement month = driver.findelement(by.xpath(".//*[@id='blue-box-           fluid-round']/center/table/tbody/tr[7]/td[2]/select[1]"));      select index1 = new select(month);      index1.selectbyvisibletext("september");      webelement date = driver.findelement(by.xpath(".//*[@id='blue-box-fluid-     round']/center/table/tbody/tr[7]/td[2]/select[2]"));      select index2 = new select(date);      index2.selectbyvisibletext("15");       webelement year = driver.findelement(by.xpath(".//*[@id='blue-box-       fluid-round']/center/table/tbody/tr[7]/td[2]/select[3]"));       select index3 = new select(year);       index3.selectbyvisibletext("1995");       webelement ethn = driver.findelement(by.xpath(".//*[@id='blue-box-        fluid-round']/center/table/tbody/tr[8]/td[2]/select"));       select index4 = new select(ethn);       index4.selectbyvisibletext(ethnicity);       webelement coun = driver.findelement(by.xpath(".//*[@id='country']"));       select index5 = new select(coun);       index5.selectbyvisibletext(country);       }           catch (exception e)              {             e.printstacktrace();             }           }         }      } 

pardon me silly mistakes, still new this. thinking making function cant seem come strategy

few things.

  1. don't use long xpaths until there no way identify element. use name or id. code break if small change happen in table html.
  2. it idea have separate webelement rather having array element , accessing them. doing good, use better identifier.

however, if interested in knowing how this, following code.

list<webelement> dropdowns = driver.findelements(by.xpath(".//*[@id='blue-box-fluid-round']/center/table/tbody/tr//select")); new select(dropdowns.get(0)).selectbyvisibletext(gender); new select(dropdowns.get(1)).selectbyvisibletext("september"); 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -