如何在selenium webdriver中从一个java类(页面)调用下拉值到另一个TestNG类(测试)?

时间:2017-11-16 12:44:20

标签: java selenium selenium-webdriver dropdown

我想在页面的一个元素(java类)中选择并存储一个下拉值,并从不同的类中调用它。

EX:我正在为所有其他领域做这样的事情

public class Home_page {

    private static WebElement element = null;

    public static WebElement txtbox_FirstName(WebDriver driver) {
        element = driver.findElement(By.id("u_0_g"));
        return element;
    }
}

public class Action_createAccount {

    private static WebDriver driver = null;

    public static void main(String args[]) {

    System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
    driver.get("https://www.facebook.com/");


    Home_page.txtbox_FirstName(driver).sendKeys("Anu");
   }
}

这是完美的。我无法编写下拉代码以相同的方式工作。请帮忙

1 个答案:

答案 0 :(得分:0)

如果您想选择出生日,示例代码就在这里。它可能对你有所帮助。

public class Home_page {
    private static WebElement element = null;

    public static WebElement txtbox_FirstName(WebDriver driver) {
        element = driver.findElement(By.id("u_0_g"));
        return element;
    }
    Public static WebElement dropdown_BirthDay_Day(WebDriver driver){
        element = driver.findElement(By.id("day"));
        return element;
    }
    Public static WebElement dropdown_BirthDay_Month(WebDriver driver){
        element = driver.findElement(By.id("month"));
        return element;
    }Public static WebElement dropdown_BirthDay_Year(WebDriver driver){
        element = driver.findElement(By.id("year"));
        return element;
    }

}

public class Action_createAccount {

    private static WebDriver driver = null;

    public static void main(String args[]) {

        System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.get("https://www.facebook.com/");


        Home_page.txtbox_FirstName(driver).sendKeys("Anu");
        new Select(Home_page.dropdown_BirthDay_Day(driver)).selectByVisibleText("16");
        new Select(Home_page.dropdown_BirthDay_Month(driver)).selectByVisibleText("Nov");
        new Select(Home_page.dropdown_BirthDay_Year(driver)).selectByVisibleText("1990");
    }
}