我被要求在我的公司突然自动化一个网站,我被困在那个我必须一个接一个地自动完成5-6个下拉列表的部分
每个下拉列表中包含5-6个webelements的代码似乎不正确
Json File::
{
"username": yawningemu3,
"password": any,
"cpassword": any,
"email": vishek3242@gmail.com,
"cemail": vishek3242@gmail.com,
"gender": Man,
"country": India,
"ethnicity": Indian
}
我似乎无法找到优化代码给定部分的方法:
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();
}
}
}
}
对于任何愚蠢的错误,请原谅我,我还是新手。
我正在考虑制定一个功能,但似乎无法达成一个好的策略
答案 0 :(得分:0)
很少有事情。
但是,如果您只是想知道如何做到这一点,请遵循以下代码。
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");