Unable to read text from textbox using gettext() of selenium webdriver?

I can't read dates from ajax calendar control after date selection

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());


    }

Answers


The call to get the input text should be:

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 input element, you are changing its value attribute.


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.

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.


In this case you should invoke getAttribute("value").

I believe getText() only returns the inner text of the element, not the inputted value.


driver.findElement(By.id("ctl00_SampleContent_Date5")).getAttribute("value");

it always return the values that has been written inside textbox


Need Your Help

Change background color of JQuery slider

jquery jquery-ui

I understand I can set the background color of the value from min to the current slider selection by doing a:

How do I include a locally defined function when using PowerShell's Invoke-Command for remoting?

powershell powershell-v2.0 powershell-remoting

I feel like I'm missing something that should be obvious, but I just can't figure out how to do this.