ajax 无法使用 selenium webdriver 的 gettext() 从文本框中读取文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11258207/
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
Unable to read text from textbox using gettext() of selenium webdriver?
提问by Jasmine.Olivra
I can't read dates from ajax calendar control after date selection
选择日期后,我无法从 ajax 日历控件中读取日期
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/Calendar/Calendar.aspx
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/Calendar/Calendar.aspx
I don't get any error but I can't fetch any value from textbox.
我没有收到任何错误,但我无法从文本框中获取任何值。
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/Calendar/Calendar.aspx");
driver.manage().window().maximize();
//Default calendar:
driver.findElement(By.xpath("//*[@id='ctl00_SampleContent_Date1']")).click();
for(int i=0;i<=5;i++){
for(int j = 0;j<=6;j++){
System.out.print(driver.findElement(By.xpath("//*[@id='ctl00_SampleContent_defaultCalendarExtender_day_"+i+"_"+j+"']")).getText()+"-");
}
System.out.println();
}
driver.findElement(By.xpath("//*[@id='ctl00_SampleContent_defaultCalendarExtender_day_3_4']")).click();
System.out.println(driver.findElement(By.xpath("//*[@id='ctl00_SampleContent_Date1']")).getText());
//Calendar with an associated button:
System.out.println("Calendar with an associated button:------------------------------------------------------");
driver.findElement(By.xpath("//*[@id='ctl00_SampleContent_Image1']")).click();
for(int i=0;i<=5;i++){
for(int j = 0;j<=6;j++){
System.out.print(driver.findElement(By.xpath("//*[@id='ctl00_SampleContent_calendarButtonExtender_day_"+i+"_"+j+"']")).getText()+"-");
}
System.out.println();
}
Thread.sleep(5000L);
driver.findElement(By.xpath("//*[@id='ctl00_SampleContent_Image1']")).click();
driver.findElement(By.xpath("//*[@id='ctl00_SampleContent_calendarButtonExtender_day_3_3']")).click();
System.out.println(driver.findElement(By.xpath("//*[@id='ctl00_SampleContent_Date5']")).getText());
}
回答by Petr Jane?ek
The call to get the inputtext should be:
获取input文本的调用应该是:
driver.findElement(By.id("ctl00_SampleContent_Date5")).getAttribute("value");
Don't ask me why, it's just the way it always has been. By typing into an inputelement, you are changing its valueattribute.
别问我为什么,一直都是这样。通过键入input元素,您正在更改其value属性。
回答by SebTardif
I think Selenium is aligned to how DOM works but still, I don't think any user expect that getText will not return what is visible in the screen. Both innerHTML, and innerText return empty string so that explain why getText doesn't return anything.
我认为 Selenium 与 DOM 的工作方式一致,但我认为没有任何用户希望 getText 不会返回屏幕中可见的内容。innerHTML 和innerText 都返回空字符串,以便解释为什么getText 不返回任何内容。
I think Selenium should simplify oddities of the DOM. In other words, getText() should return what a user see in the screen, then if a use case want the real innerText, they can call other API.
我认为 Selenium 应该简化 DOM 的奇怪之处。换句话说,getText() 应该返回用户在屏幕上看到的内容,然后如果用例想要真正的innerText,他们可以调用其他API。
回答by ndarriulat
In this case you should invoke getAttribute("value").
在这种情况下,您应该调用getAttribute("value").
I believe getText()only returns the inner text of the element, not the inputted value.
我相信getText()只返回元素的内部文本,而不是输入的值。
回答by Rahul
driver.findElement(By.id("ctl00_SampleContent_Date5")).getAttribute("value");
driver.findElement(By.id("ctl00_SampleContent_Date5")).getAttribute("value");
it always return the values that has been written inside textbox
它总是返回已写入文本框的值

