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

时间:2020-06-29 09:52:56

标签: html selenium xpath webdriverwait partiallinktext

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

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

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

enter image description here

下面是硒铬浏览器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

上传了整个网页View Source

问题不在于页面加载和页面等待。实际上,该页面已加载,但单击“请求”时显示的下拉列表没有发生(单击)

运行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 个答案:

答案 0 :(得分:0)

有一个空格,这就是为什么硒无法通过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();

答案 1 :(得分:0)

您需要考虑以下几点:

  • 文本为请求<a>元素有一个onclick事件,因此它是启用了JavaScript的元素。
  • <a>节点,其文本为安全测试请求异常请求变更请求退役请求它们都具有领先的空格字符。因此linkText无法正常工作。

解决方案

要单击文本为 Requests 的元素,然后单击文本为 Security Test Requests 的元素,则需要使用WebDriverWait作为{{ 1}},则可以使用以下任一Locator Strategies

  • elementToBeClickable()

    partialLinkText
  • new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='tv_3']/a/i"))).click(); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Security Test Requests"))).click();

    xpath