0
我剛剛開始使用自動化功能,並且遇到了如何在3種不同的下拉菜單中輸入不同xpath的日期,月份和年份以更有效的方式,這樣我就不必使用select類爲他們如何更有效地在3個不同的下拉菜單中輸入日期,月份和年份
這裏的每一個是代碼:
package com.singh.assignment;
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.Select;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Json
{
public static void main(String args[])
{
JsonParser parser = new JsonParser();
try
{
Object obj = parser.parse(new FileReader("C:\\Users\\dell\\eclipse-
workspace\\Assignment\\data.json"));
JsonObject jsonObject = (JsonObject) obj;
String fname = (String) jsonObject.get("fname").getAsString();
String lname = (String) jsonObject.get("lname").getAsString();
String baseurl = (String) jsonObject.get("baseurl").getAsString();
String mstatus = (String) jsonObject.get("mstatus").getAsString();
System.setProperty("webdriver.gecko.driver","E:\\WORK\\geckodriver.exe\\");
WebDriver driver = new FirefoxDriver();
driver.get(baseurl);
driver.findElement(By.partialLinkText("Registration")).click();
driver.findElement(By.xpath("//input[@id =
'name_3_firstname']")).sendKeys(fname);
driver.findElement(By.xpath("//input[@id =
'name_3_lastname']")).sendKeys(lname);
List<WebElement> martial = driver.findElements(By.name("radio_4[]"));
{
for(WebElement radio : martial)
{
if(radio.getAttribute("value").equalsIgnoreCase(mstatus))
{
radio.click();
}
}
}
driver.findElement(By.xpath("//input[@value = 'reading']")).click();
WebElement cntry = driver.findElement(By.xpath("//select[@id =
'dropdown_7']"));
Thread.sleep(3000);
Select index = new Select(cntry);
index.selectByVisibleText("India");
WebElement month = driver.findElement(By.id("mm_date_8"));
Select index1 = new Select(month);
index1.selectByVisibleText("9");
WebElement date = driver.findElement(By.id("dd_date_8"));
Select index2 = new Select(date);
index2.selectByVisibleText("15");
WebElement year = driver.findElement(By.id("dd_date_8"));
Select index3 = new Select(year);
index3.selectByVisibleText("1995");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
這裏是JSON文件:
{
"baseurl": "http://demoqa.com/",
"fname": AKASHDEEP,
"lname": SINGH,
"mstatus": single,
"hobby": ["Dance", "Reading", "Cricket"]
}
比什麼更有效的方法?你沒有發佈你的代碼。 – Guy
比爲每個日期月份和年份的下拉列表選擇一個班級效率更高 –
將您的代碼與HTML源樣本一起分享 – Andersson