2

我使用 Internet Explorer 作为浏览器来自动化测试,使用 webdriver。我的问题是,如何在 div 类下找到文本?

我的 HTML 如下所示:

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

我曾尝试使用这样的 XPath

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

但我收到如下所示的错误:

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)
4

2 回答 2

1

您的 XPATH 错误:

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

它应该是:

driver.findElement(By.xpath("//div[@class='welcome_text']"));
于 2013-04-17T14:29:44.373 回答
1

尝试 By.className() 方法,如下所示。

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

webelement.getText();
于 2013-04-16T10:43:30.510 回答