从Selenium Webdriver WebElement字段中检索值并将其传递给Java变量 [英] Retrieving the value from a Selenium Webdriver WebElement field and passing it to a java variable

查看:96
本文介绍了从Selenium Webdriver WebElement字段中检索值并将其传递给Java变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,请谅解初学者的问题.我是Java和Selenium Webdriver的相对新手.

Firstly, forgive the beginner question. I'm a relative newcomer to both Java and Selenium Webdriver.

我正在通过尝试测试CMS应用程序来学习Webdriver. CMS具有创建文章并通过工作流发送文章的概念.因此,编辑者可以创建文章并将其发送给超级用户.因此,我将创建一个文章作为编辑器,并使用sendKeys文本并附加创建日期来填充标题"字段:

I am learning Webdriver through trying to test a CMS application. The CMS has the concept of creating articles and sending them through a workflow. So, an editor might create an article and send through to a SuperUser. So, I am creating an article as an editor and am populating the 'Title' field by using sendKeys text and appending the date of creation:

public class EditorArticleCreator {

    private static WebDriver driver;
    private static String baseURL = TestEnv.getUrl();
    static WebDriverWait wait;
    Date date = new Date();
    static String articleName;

    public static String getArticleName(){
        return articleName;
    }

//some code and then:

driver.findElement(By.cssSelector("#Article_Title")).sendKeys("New_Article_" + date.toString());

我想做的是将创建的Title的String值传递到一个变量中,然后可以在SuperUser的类中检索该变量(通过Get方法).

What I want to do is pass the String value of the created Title into a variable that I can then retrieve in the class of the SuperUser (via a Get method).

public class EditArticleSuperUser {

    EditorArticleCreator.getArticleName();

}

这样,我希望当我以SuperUser身份登录WebDriver时,他们会选择正确的文章,因为完整的String将存储在articleName字段中.

This way, I hope that when I have WebDriver login as the SuperUser, they will pick the correct article as the full String will be stored in the articleName field.

这是最好的方法吗?我如何从driver.findElement中获取String值?

Is this the best way? How would I get the String value from the driver.findElement?

推荐答案

您可以使用getAttribute获取文本输入的value属性:

You could use getAttribute to get the value attribute of the text input:

WebElement articleTitleField = driver.findElement(By.cssSelector("#Article_Title"));
articleTitleField.sendKeys("New_Article_" + date.toString());
articleName = articleTitleField.getAttribute("value");

或者MrTi的对articleTitleField.getText()的建议也会起作用!

Or MrTi's suggestion of articleTitleField.getText() would work too!

这篇关于从Selenium Webdriver WebElement字段中检索值并将其传递给Java变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆