selenium - 给定 xpath 表达式 "//div[contains(@class=' loader-overlay')]"对 Selenium 无效

标签 selenium xpath geckodriver selenium-firefoxdriver

从 2.53 版升级到 3.14 版后,Selenium 说

给定的 xpath 表达式“//div[contains(@class='loader-overlay')]”无效

这段代码

System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver/v0.23.0");
FirefoxOptions options = new FirefoxOptions();
//...
driver = new FirefoxDriver(options);
WebElement loaderElement = driver.findElement(By.xpath("//div[contains(@class='loader-overlay')]"));

产生这个错误

org.openqa.selenium.InvalidSelectorException: Given xpath expression "//div[contains(@class='loader-overlay')]" is invalid: [Exception... "<no message>"  nsresult: "0x8060000d (<unknown>)"  location: "JS frame :: chrome://marionette/content/element.js :: element.findByXPath :: line 401"  data: no]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
System info: host: 'xxx', ip: '10.233.112.79', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-116-generic', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 64.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.23.0, moz:headless: false, moz:processID: 2736, moz:profile: /tmp/rust_mozprofile.OP8KKQ..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, platformVersion: 4.4.0-116-generic, rotatable: false, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}

最佳答案

你需要更换

//div[contains(@class='loader-overlay')]

//div[contains(@class, 'loader-overlay')]

请注意,如果您想检查属性值是否恰好是 “value”,您应该使用 [@attr = "value"] 语法,但是 contains 语法是 [contains(@attr, "value")]

关于selenium - 给定 xpath 表达式 "//div[contains(@class=' loader-overlay')]"对 Selenium 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54217485/

相关文章:

java - 使用 Selenium Java 无法按名称包含找到复选框元素

python - 使用python在Gmail中嵌入带有图像路径的html

php - 使用 XPath 和 PHP 的 SimpleXML 查找包含字符串的节点

python-3.x - geckodriver 的 Google Colaboratory 中的 executable_path 是什么?

python - 更改 Geckodriver for Python Selenium 的日志级别

python - python 的 selenium 的导入错误

由 xpath : search by text in child elements 提供的 Selenium WebDriver

html - 包含具有特定文本的另一个元素的元素的 XPath?

xml - XPath 表达式获取所有前面的兄弟 UNTIL 条件满足

python - 网络驱动程序异常 : Message: connection refused