硒:没有这样的元素

时间:2017-07-16 09:14:06

标签: java selenium webdriver

考虑this page。我有兴趣使用selenium webdriver获取祝贺文本。这是经过尝试的代码:

driver.findElement(By.xpath("/html/body/font/strong/em/text()")).getText().contains(message);

但它抱怨:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/font/strong/em/text"}
  (Session info: chrome=59.0.3071.115)
  (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 6.1.7600 x86) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 130 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'salman-PC', ip: '192.168.1.5', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41), userDataDir=C:\Users\salman\AppData\Local\Temp\scoped_dir7612_3059}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=59.0.3071.115, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 9ffe01502a1cd688cd022bf5e7719e42
*** Element info: {Using=xpath, value=/html/body/font/strong/em/text}
    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:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
    at ee.ignite.pages.SelectFromDropDownPage.getText(SelectFromDropDownPage.java:33)
    at ee.ignite.steps.SelectFromDropDown_Steps.i_can_see_the_maessage(SelectFromDropDown_Steps.java:32)
    at ✽.Then I can see the maessage "Congratulations"(D:/Workspace/IgniteTask/src/features/SelectFromDropDown.feature:9)
似乎路径

/html/body/font/strong/em/text()

错了?

3 个答案:

答案 0 :(得分:1)

  • 取决于浏览器和连接的性能,加载页面内容所花费的时间将有所不同。 因此,请花一些时间来加载它们。 在这里,如果您在网页上使用JS,建议您使用显式等待。 这样,我们可以为Web驱动程序定义一个时间量,该时间量要等到满足某些要求后才能执行下一行代码。

在您的情况下:-

  WebDriver webdriver = new FirefoxDriver();
  webdriver .get("Your URL");
  WebDriverWait wait = new WebDriverWait(webdriver,30);
  WebElement element = wait.untill(ExpectedConditions.visibilityOfElementLocated(By.xpath("your X-path")));
  • 根据此示例代码,您的Web驱动程序将等待30秒,直到可见命名对象为止。

答案 1 :(得分:0)

findElement()方法返回WebElementxpath "/html/body/font/strong/em/text()"会返回String。我猜你已收到InvalidSelectorException,所以你尝试了xpath "/html/body/font/strong/em/text",根据堆栈跟踪,它不存在,所以你得到了NoSuchElementException

driver.findElement(By.xpath("/html/body/font/strong/em")).getText();

应该给你文字。

  

修改

要让driver一段时间找到元素以防止出现时间问题,您可以设置implicit wait

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath("/html/body/font/strong/em")).getText();

答案 2 :(得分:0)

为了获取文本,您需要先将其存储在一些可根据需要打印或使用的变量中。以下是示例代码。

scanf

我在我的机器上检查了它并且工作正常。