当前位置: 首页 > 知识库问答 >
问题:

无法使用selenium webdrive单击带有和标签的列表元素

符畅
2023-03-14

我有一个nav标签中的元素列表。我想单击第一个列表元素。我的HTML如下所示:

<aside id="left-panel" style="overflow: visible;">
   <div id="selectedBrand" class="custum-brand-info login-info">
      <span class="dropdown">
    </div>
    <nav>
       <ul id="sideNavigation">
          <li>
             <a onclick="showHideBrandmanagement(true);" href="/sentiment/view">
                <i class="fa fa-lg fa-fw fa-thumbs-up"></i>
                <span class="menu-item-parent">Sentiment</span>
             </a>
          </li>
       </ul>
    </nav>

我的测试用例有时会失败;我的测试用例通过了,但有时失败了。我正在使用XVFB以无头模式运行我的测试用例。

应该获取主题*FAILED*中的数据

[info]   org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"#left-panel nav #sideNavigation li:nth-child(9) #rAnalytics"}
[info] Command duration or timeout: 10.06 seconds
[info] For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
[info] Build info: version: '2.47.1', revision: 'unknown', time: '2015-07-30 11:02:44'
[info] System info: host: 'knoldus-Vostro-2520', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.19.0-30-generic', java.version: '1.7.0_80'
[info] *** Element info: {Using=css selector, value=#left-panel nav #sideNavigation li:nth-child(9) #rAnalytics}
[info] Session ID: edc0a525-38bb-48b2-88c4-e1ad01265c15
[info] Driver info: org.openqa.selenium.firefox.FirefoxDriver
[info] Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=41.0}]
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[info]   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[info]   at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[info]   at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
[info]   at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
[info]   at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
[info]   at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
[info]   at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:437)
[info]   at selenium.professionalPlan.Topic$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(Topic.scala:31)
[info]   ...
[info]   Cause: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"#left-panel nav #sideNavigation li:nth-child(9) #rAnalytics"}
[info] For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
[info] Build info: version: '2.47.1', revision: 'unknown', time: '2015-07-30 11:02:44'
[info] System info: host: 'knoldus-Vostro-2520', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.19.0-30-generic', java.version: '1.7.0_80'
[info] Driver info: driver.version: unknown
[info]   at .FirefoxDriver.prototype.findElementInternal_(file:///tmp/anonymous5218088375039490898webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10667)
[info]   at .fxdriver.Timer.prototype.setTimeout/<.notify(file:///tmp/anonymous5218088375039490898webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:623)
[1]

共有1个答案

马高谊
2023-03-14

如果只想找到一个id,请使用

//nav[1]/ul[@id='sideNavigation']

但上面的xpath将返回许多元素。

如果您正在寻找包含"Sentiment"文本的元素,请使用下面的Xpath:-

//nav[1]/ul[@id='sideNavigation']//span[contains(text(),'Sentiment')]

如果它仍然不工作,然后检查您的页面中是否有任何框架。如果该框架包含上述元素的超文本标记语言代码,那么您需要先切换到框架

请尝试使用以下代码:-

WebElement elem = driver.findElement(By.xpath("//nav[1]/ul[@id='sideNavigation']//span[contains(text(),'Sentiment')]"));
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript(js, elem);;

希望它能帮助您:)

 类似资料:
  • 我试图点击一个按钮,它出现在图的右侧(图上方的三行),最后想点击“下载为CSV”。我的代码如下。 File=WebDriverWait(驱动程序,20).until(ec.element_to_be_clickable((by.xpath,'//*[@id=“HighCharts-3SLSA4F-0”]/div[2]/ul/li[6]')).click()#下载为csv 错误:引发TimeoutE

  • 我试图创建一个自动的网络会话,我登录到一个网站,并从下拉框中选择一个选项。我能够使用硒进入页面,但我无法点击打开下拉菜单的栏,然后选择我想要的选项。这是页面的截图,它的超文本标记语言代码:[![在此输入图像描述][1]][1][![在此输入图像描述][2]][2] 我想单击下拉列表中的“降级性能”选项。我有点被困在这里,因为下拉列表的HTML与[this one][3]根本不相似(无法“选择”)。

  • 我正在尝试使用selenium WebDriver实现应用程序的自动化。我经常遇到click action在WebElements上不起作用的情况。 例如,在html块中,类似于: 我已经试着把这件事做了差不多一个星期了。谁能帮帮我吗? 代码似乎确实改变了单击时的图像(我比较了单击前后的图像源)。但是没有可见的“onclick()”调用(我不确定是否有其他方法在click上调用java脚本函数,如

  • 在此输入图像描述无法使用selenium webdriver单击内联元素。 这是URL 除了图像链接(右侧顶部),还有一个方形图标。需要单击该图标并选择“地图”。附上截图。我使用了xpath、cssselector、ID、Name,但没有任何效果。谁能帮我一下吗。 代码:

  • 问题内容: 我目前正在从事一个自动填写表格的项目。填写表单后,出现下一个按钮,这就是为什么它给我一个错误。 我努力了: HTML: 错误: selenium.common.exceptions.ElementClickInterceptedException:消息:元素单击被拦截:元素在点(203,530)不可单击。其他元素将获得点击:…(会话信息:chrome = 76.0.3809.132)

  • 问题内容: 我试图单击“所有主题”和“所有状态”复选框,然后搜索结果。当我运行脚本时,会打开一个Chrome窗口,大小为1036x674。 如果我不理会窗口,则会出现元素点击拦截错误。如果我最小化或最大化窗口,则脚本可以正常工作。 我正在使用Selenium 3.141.0,chrome 76,chromedriver 76和python 3.6 但是我得到这个错误: ElementClickIn