user2286039
user2286039

Reputation: 29

How to find the text under div class

I am using Internet Explorer as a browser to automate testing, using webdriver. My question being, how do I find the text under the div class?

My HTML looks like this:

<div class="welcome_text">
Text-Welcome Vijayanand

I have tried to use an XPath like this

WebElement webelement=driver.findElement(By.xpath("//div[@Class='welcome_text']/div"));
webelement.getText();

But I am getting the error shown below:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //DIV[@Class='welcome_text']/DIV (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 656 milliseconds

For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

Build info: version: '2.32.0', revision: '6c40c18', time: '2013-04-09 17:23:22'

System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_05'

Session ID: 87815f2f-f6ff-4689-ac34-befb114acfe4

Driver info: org.openqa.selenium.ie.InternetExplorerDriver

Capabilities [{platform=WINDOWS, elementScrollBehavior=0, javascriptEnabled=true, enablePersistentHover=true, ignoreZoomSetting=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=8, cssSelectorsEnabled=true, ignoreProtectedModeSettings=true, requireWindowFocus=false, allowAsynchronousJavaScript=true, handlesAlerts=true, initialBrowserUrl=, nativeEvents=true, takesScreenshot=true}]

      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

      at java.lang.reflect.Constructor.newInstance(Unknown Source)

      at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187)

      at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)

      at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)

      at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)

      at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:404)

      at org.openqa.selenium.By$ByXPath.findElement(By.java:344)

      at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)

      at internetexplorerlaunch.IElaunch.main(IElaunch.java:44)

Upvotes: 2

Views: 530

Answers (2)

buddy
buddy

Reputation: 183

Your XPATH is wrong:

WebElement webelement=driver.findElement(By.xpath("//div[@Class='welcome_text']/div"));
webelement.getText(); 

It should be:

driver.findElement(By.xpath("//div[@class='welcome_text']"));

Upvotes: 1

HemChe
HemChe

Reputation: 2337

Try By.className() method as shown below.

WebElement webelement=driver.findElement(By.className("welcome_text"));

webelement.getText();

Upvotes: 1

Related Questions