【问题标题】:Selenium: No such element硒:没有这种元素
【发布时间】:2017-07-16 09:14:06
【问题描述】:

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

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

错了吗?

【问题讨论】:

    标签: java selenium webdriver


    【解决方案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")));
    
    • 根据此示例代码,您的网络驱动程序将等待 30 秒的时间,直到命名对象可见。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        String t1 = driver.findElement(By.xpath("/html/body/font/strong/em")).getText();
        System.out.println(t1);
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-11-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-25
          相关资源
          最近更新 更多