【问题标题】:Issue with finding element in Selenium webdriver. Using webdriver.gecko.driver在 Selenium webdriver 中查找元素的问题。使用 webdriver.gecko.driver
【发布时间】:2017-08-28 09:10:57
【问题描述】:

我能够在以下版本的 webdriver 中找到该元素,但最新版本无法找到该元素 driver.findElement(By.id("dropdownMenu")).click();

在控制台中获取:

    1491224447464   geckodriver INFO    Listening on 127.0.0.1:14273
1491224448607   mozprofile::profile INFO    Using profile path C:\Users\COMPIN~1\AppData\Local\Temp\rust_mozprofile.btmAM70uon4k
1491224448614   geckodriver::marionette INFO    Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe with args []
1491224448633   geckodriver::marionette INFO    Connecting to Marionette on localhost:65438
1491224450952   Marionette  INFO    Listening on port 65438
Apr 03, 2017 6:30:53 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
JavaScript warning: https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js, line 1: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
JavaScript error: https://qa.xyz.com/assets/js/custom.js?1491224506, line 12601: TypeError: $(...).select2 is not a function
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: #dropdownMenu
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: '5234b32', time: '2017-03-10 09:00:17 -0800'
System info: host: 'CO-PC', ip: '169.254.250.92', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\CO~1\AppData\Local\Temp\rust_mozprofile.btmAM70uon4k, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0, platformVersion=6.1, moz:processID=6952, browserName=firefox, platformName=windows_nt}]
Session ID: 76cd7017-8b8b-463f-84a4-c580b6a58bf8
*** Element info: {Using=id, value=dropdownMenu}
    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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:133)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:99)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:43)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:604)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:420)
    at org.openqa.selenium.By$ById.findElement(By.java:218)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at WebApp.mc.main(mc.java:29)

【问题讨论】:

  • 我也看到了 JS 执行错误。这是造成问题吗?元素是否按预期显示?
  • 使用相同的代码行,我在朋友圈尝试了较低版本的 webdriver,它工作正常。我已经在这里粘贴了所有控制台输出@TatsuyukiIshi
  • @Rick 如果您使用的是最新版本的 geckodriver,请确保您的 firefox 浏览器也是最新的,不要使用旧的 firefox 版本和最新的 geckodriver
  • @ShoaibAkhtar 我的浏览器是最新的 53.0b8(32 位)
  • 用其他定位器(如 css 或 xpath)试一试,看看它在这种情况下是否适用于您和您朋友的笔记本电脑。您可以按照此链接生成 xpath-learn-automation.com/how-to-write-dynamic-xpath-in-selenium

标签: java selenium firefox selenium-webdriver geckodriver


【解决方案1】:

请确保以下几点:

  1. 您使用的是最新版本的 Selenium。 v 3.x.x 和最新版本的 gecko 驱动程序以及最新版本的 Mozilla Firefox 浏览器。

  2. 要使用 Selenium 3.x.x,您需要下载适当的 gecko 驱动程序。

  3. 在编写代码时,您需要在开头提及以下内容:

System.setProperty("webdriver.gecko.driver","C:\\your_location\\geckodriver.exe");

  1. 然后继续初始化您的驱动程序。

  2. 访问网址后,请引入 ImplicitlyWait 以加载 ajax 扩展和 js 扩展。

  3. 由于错误为“NoSuchElementException”,请检查是否可以将该 ID 标识为唯一元素。

  4. 否则尝试使用其他属性,例如 name/css/xpath。

如果这对你有帮助,请告诉我。

【讨论】:

  • 1. gecko 驱动程序和 Mozilla firefox 更新了一个。 2. 有合适的司机。 3. 我已经设置好了,否则会显示错误。
  • @Rick 你能分享相关的 HTML DOM 和你的代码吗?
  • 我在哪里可以获得 Html DOM
  • public class mc { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe");驱动程序=新的 FirefoxDriver(); driver.get("xyz/"); driver.findElement(By.xpath("html/body/nav/div/div[2]/div[2]/ul/li/a")).click(); Thread.sleep(5000); driver.findElement(By.id("email")).sendKeys("qwerty"); driver.findElement(By.xpath(".//*[@id='pwd']" )).sendKeys("qwerty"); driver.findElement(By.xpath(".//*[@id='prd_login']")).click(); } }
  • 因为你的错误是:NoSuchElementException: Unable to locate element: #dropdownMenu 所以你能更新那部分代码吗?
猜你喜欢
  • 2021-07-04
  • 1970-01-01
  • 2017-09-12
  • 2017-07-03
  • 2014-07-18
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多