开发者问题收集

无法点击使用 Selenium 和 Java 的网站上的链接

2020-06-29
507

我希望使用 selenium Java 单击网站左侧面板中的“请求”链接。

请参阅下面的快照,其中 xpathfinder 显示元素为:

/html/body/div[1]/nav/div/section/ul[3]/li/a/span[1]

在此处输入图像描述

下面是 selenium chrome 浏览器 IDE 生成的我的 java 代码的一部分。

System.out.println("Title of the page is 7 -> Login");
driver.findElement(By.xpath("//li[@id='tv_3']/a/i")).click();
driver.findElement(By.linkText("Security Test Requests")).click();
driver.findElement(By.id("w0")).click();

下面是页面的查看源代码,其中代码的相关部分具有请求链接。

</ul>
<ul class="sidebar-menu">
<li class="treeview" id='tv_3'>
<a href="#">
<i class="fa fa-edit"></i> <span>Requests</span>
<span class="pull-right-container" onclick="menu_dropdown('tv_3');"><i class="fa fa-angle-left pull-right"></i></span>
</a>
<ul class="treeview-menu">
<li><a href="/synvm/basic/web/index.php?r=request%2Findex-test-request"> Security Test Requests</a></li>
<li><a href="/synvm/basic/web/index.php?r=request%2Findex"> Exception Requests</a></li>
<li><a href="/synvm/basic/web/index.php?r=request%2Findex-change-request"> Change Requests</a></li>
<li><a href="/synvm/basic/web/index.php?r=request%2Findex-decommission-request"> Decommission Requests</a></li>
</ul>
</li>
</ul>

编辑:在此处上传了网页的整个 查看源代码 https://drive.google.com/file/d/1ov4Es3oDC66coaJADw6N1kxTorLmd3M6/view?usp=sharing

问题不在于页面加载和页面等待。实际上,页面已加载,但点击“请求”会显示一个下拉菜单,但实际上并没有发生(点击)

我在运行 JAVA 时收到以下错误:

Title of the page is 7 -> Login
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Security Test Requests"}
  (Session info: chrome=75.0.3770.100)
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'VMINITSERMAPRAP', ip: '10.9.40.115', os.name: 'Windows Server 2016', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Users\axmwiis\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:64908}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 3ca2879b253b55a953c0a481ec70ea78
*** Element info: {Using=link text, value=Security Test Requests}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

根据这篇文章中的建议,我尝试了以下操作:

new WebDriverWait(driver, 40).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='sidebar-menu']/li[@class='treeview']/a//span[text()='Requests']"))).click();

但是,我收到以下错误:

Title of the page is 7 -> Login
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //ul[@class='sidebar-memu']/li[@class='treeview']/a//span[text()='Requests'] (tried for 40 second(s) with 500 milliseconds interval)
        at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
        at pack.SynvmRequest.testSynvmRequest(SynvmRequest.java:135)
        at pack.SynvmRequest.main(SynvmRequest.java:398)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//ul[@class='sidebar-memu']/li[@class='treeview']/a//span[text()='Requests']"}
  (Session info: chrome=75.0.3770.100)
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'MyHost', ip: '10.9.140.15', os.name: 'Windows Server 2016', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Usersxmwiis\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:53969}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 979b2c191659cbbba7c43ebe4db9b5b9
*** Element info: {Using=xpath, value=//ul[@class='sidebar-memu']/li[@class='treeview']/a//span[text()='Requests']}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

您能提出建议吗?

2个回答

有一个空格,这就是为什么 selenium 无法通过 linktext("Security Test Requests") 检测到您的元素

<li><a href="/synvm/basic/web/index.php?r=request%2Findex-test-request"> Security Test Requests</a></li>

您的元素应该是

driver.findElement(By.linkText(" Security Test Requests")).click();

或者您可以使用包含 XPath 的方法

 driver.findElement(By.xpath("//li[contains(text(), 'Security Test Requests')]").click();
Norayr Sargsyan
2020-06-29

您需要考虑以下几个事情:

  • &lt; a&gt; 带有文本的 请求 具有 onclick 事件,因此它是 javascript 启用元素。
  • &lt; a&gt; 带有文本的节点 安全测试请求 例外请求 更改请求 和< em>退役请求 所有这些都具有领先的白空间字符。因此 linkText 无法正常工作。

解决方案

单击带有 请求的文本元素 ,然后在元素上使用文本作为 安全测试请求< /strong>您需要使用 webdriverwait 对于 element elementTobeClickable() ,您可以使用以下任何一种 定位器策略

  • partialLinkText

     new WebDriverWait(driver,20).until(ExpectionConditions.elementTobeClicklickable(by.xpath) ='tv_3']/a/i”))。单击();
    new WebDriverWait(驱动程序,20).ultil(endivConditions.elementTobeclickable(by.partiallinktext(“安全测试请求”)))。单击(); 
  • xpath

     new WebDriverWait(driver,20).until(ExpectionConditions.elementTobeClicklickable(by.xpath)(by.xpath(“ // ul [@class) ='sidebar-menu']/li [@class ='treeview']/a // span [text()='requests']”))。单击();
    new WebDriverWait(驱动程序,20).until(ExpectConditions.elementTobeClickable(by.xpath)(“ // ul [@class ='treeview-menu'] // li/a [包含(。 )))。点​​击(); 
undetected Selenium
2020-06-29