如何使用 selenium webdriver & Java 获取文本框值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28980627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:29:35  来源:igfitidea点击:

How to get a textbox value using selenium webdriver & Java?

javaselenium

提问by chandra sekhar

1) I have entered name in "Full Name" text box field.

1) 我在“全名”文本框字段中输入了姓名。

 WebElement name = driver.findElement(By.id("userId"));
        name.sendKeys("Sekhar");

it has entered value in the textbox.

它已在文本框中输入值。

2) I have to validate that "Sekhar" is presented in the textbox. but here the problem is the value entered in the textbox is displaying in watermark logger.info(name.getText());does not returning a value

2)我必须验证“Sekhar”是否出现在文本框中。但这里的问题是在文本框中输入的值显示在水印 logger.info(name.getText());中没有返回值

because it is displayed in watermark in the textbox.

因为它显示在文本框中的水印中。

please help me how to get a watermark text in textbox.

请帮助我如何在文本框中获取水印文本。

回答by alecxe

It would depend on what do you mean by watermark and what is the HTML representation of the input element. But, this should be either a valueattribute:

这将取决于您所说的水印是什么意思以及输入元素的 HTML 表示是什么。但是,这应该是一个value属性:

name.getAttribute('value');

or could be a placeholder:

或者可以是placeholder

name.getAttribute('placeholder');

回答by user7506035

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('xxxx').style.display='block';");