无法找到下拉菜单的元素(写在引导带中)

时间:2019-03-30 22:44:21

标签: selenium xpath dropdown

我已经使用各种定位器来定位下拉列表的Web元素,但是每次遇到此错误时它都无法正常工作: 无法找到元素

我正在尝试给定的URL登录并选择日历 “ https://classic.crmpro.com/

在使用隐式等待后,我使用以下方法进行定位

driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);
  1. driver.findElement(By.xpath(“ // a [@href ='https://classic.crmpro.com/system/index.cfm?action=calendar&sub=default']”)))。click();

  2. driver.findElement(By.xpath(“ // * [@ id ='navmenu'] / ul / li [2] / a”))。click();

  3. driver.findElement(By.xpath(“ // * [@ title ='Calendar']”))。click();

  4. driver.findElement(By.xpath(“ // * [包含(text(),'Calendar')]”))))。click();

  5. driver.findElement(By.xpath(“ // * [@ title ='Calendar']”)))。click(); Thread.sleep(1000);

每次都会抛出类似以下的错误:

Starting ChromeDriver 2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1) on port 12336
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Mar 31, 2019 3:45:38 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@title='Calendar']"}
  (Session info: chrome=73.0.3683.86)
  (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'YESHI', ip: '192.168.0.103', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.46.628402 (536cd7adbad73a..., userDataDir: C:\Users\HP\AppData\Local\T...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:50027}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 73.0.3683.86, webStorageEnabled: true}
Session ID: 5e04eddb3f57b98161681cab972a5b2a
*** Element info: {Using=xpath, value=//*[@title='Calendar']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at YeshiProject.demo.main(demo.java:34)

请让我知道我该怎么办。

1 个答案:

答案 0 :(得分:0)

您的问题解决了吗?如果没有,请执行此操作。

您要表示的元素位于iframe内,因此您必须先进行切换。

这是iframe,我的意思是:By.name("mainpanel")

登录后,您可以执行以下操作:

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("mainpanel")));
driver.findElement(By.xpath("//*[@title='Calendar']")).click();

它将单击calendar,但是如果您只想移动到element并显示下拉菜单,请执行以下操作:

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("mainpanel")));
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.xpath("//*[@title='Calendar']"))).perform();

正在导入:

import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.interactions.Actions;

对不起,我建议更改您的凭据并将其从此站点中删除。

希望这会有所帮助。