Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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


    }
like image 293
Jasmine.Olivra Avatar asked Jun 29 '12 08:06

Jasmine.Olivra


People also ask

How do you getText from a text box in Selenium?

We can get the entered text from a textbox in Selenium webdriver. To obtain the value attribute of an element in the html document, we have to use the getAttribute() method. Then the value is passed as a parameter to the method. Let us consider a textbox where we entered some text and then want to get the entered text.

Is getText () a WebElement method?

What Is getText() Method? The Selenium WebDriver interface has predefined the getText() method, which helps retrieve the text for a specific web element. This method gets the visible, inner text (which is not hidden by CSS) of the web-element.

What is the purpose of getText () and getAttribute ()?

The getText() method simply returns the visible text present between the start and end tags (which is not hidden by CSS). The getAttribute() method on the other hand identifies and fetches the key-value pairs of attributes within the HTML tags.

In which interface getText () and getAttribute () is available?

getText() is a method which gets us the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing white space. The getAttribute() method is declared in the WebElement interface, and it returns the value of the web element's attribute as a string.


1 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.

like image 85
Petr Janeček Avatar answered Oct 14 '22 03:10

Petr Janeček