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

查看:989
本文介绍了从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();

}

这样,我希望当我有WebDriver登录超级用户,他们将选择正确的文章作为完整的字符串将存储在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天全站免登陆