【问题标题】:Unable to click a link on a website using Selenium and Java无法单击使用 Selenium 和 Java 的网站上的链接
【发布时间】:2020-10-19 11:06:15
【问题描述】:

我希望使用 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>

编辑:将网页的整个View Source上传到这里: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)

你能推荐一下吗?

【问题讨论】:

    标签: html selenium xpath webdriverwait partiallinktext


    【解决方案1】:

    有一个空格,这就是 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();
    

    【讨论】:

    【解决方案2】:

    您需要考虑以下几点:

    • 带有 Requests 文本的 &lt;a&gt; 元素有一个 onclick 事件,因此它是一个启用了 JavaScript 的元素。
    • &lt;a&gt; 节点带有文本 Security Test RequestsException RequestsChange RequestsDecommission Requests它们都有一个前导的空白字符。所以linkText 不起作用。

    解决方案

    要单击文本为 Requests 的元素,然后单击文本为 Security Test Requests 的元素,您需要将 WebDriverWait 用于 elementToBeClickable() 和您可以使用以下任一Locator Strategies

    • 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:

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

    【讨论】:

    • 我在尝试您的 Requests 解决方案时遇到错误,我现在已在我的原始帖子中更新了该解决方案。你能推荐一下吗?
    • @Ashar 检查 Request 元素是否在 &lt;iframe&gt;
    • 我不太确定 iframe @DebanjanB 所以我在这里上传了整个网页视图源代码:drive.google.com/file/d/1ov4Es3oDC66coaJADw6N1kxTorLmd3M6/…。你能建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多