简体   繁体   English

Selenium Webdriver(Java)无法单击AJAX按钮

[英]Selenium Webdriver (Java) cannot click the AJAX button

I have a website with some buttons on the top. 我有一个网站,顶部有一些按钮。 When clicked, a new AJAX section will appear. 单击后,将出现一个新的AJAX部分。

I am not too familiar with HTML, but the button "Add" (this button is my target) is somehow located in <span> . 我对HTML不太熟悉,但是按钮“添加”(此按钮是我的目标)位于<span>

    <!DOCTYPE html>
<html slick-uniqueid="3">
<head></head>
<body class="newui">
    <div class="pagewrapper ">
        <div class="section shadow" data-form-element="SectionHeading">
            <div class="section-header">
                <div class="section-title"></div>
                <div class="section-nav">
                    <ul>
<li class="add u-yellow">
<span></span>
<span style="background-color: transparent;">
    Add
</span>

I use this code in Selenium 我在硒中使用此代码

 driver.findElement(By.xpath("//li[5]/span[2]")).click();

but Selenium cannot find that button nor click it. 但Selenium无法找到该按钮或单击它。

It return error 返回错误

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (1776, 32). 线程“主要” org.openqa.selenium.WebDriverException中的异常:未知错误:元素在点(1776,32)不可单击。 Other element would receive the click: (Session info: chrome=40.0.2214.115) (Driver info: chromedriver=2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf),platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 51 milliseconds 其他元素将收到单击:(会话信息:chrome = 40.0.2214.115)(驱动程序信息:chromedriver = 2.14.313457(3d645c400edf2e2c500566c9aa096063e707c9cf),平台= Windows NT 6.3 x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:51毫秒

Can anyone help me please? 谁能帮我吗?

Thanks. 谢谢。

li元素实际上是可点击的:

driver.findElement(By.cssSelector("li.add.u-yellow")).click();

I found a good solution. 我找到了一个很好的解决方案。 Basically I used FirePath (Firefox Add-on) to find the CSS or XPath location and use it for my webdriver code. 基本上,我使用FirePath(Firefox附加组件)查找CSS或XPath位置,并将其用于我的webdriver代码。 Firepath told me that the button has CSS location Firepath告诉我该按钮具有CSS位置

.add.u-yellow>span

and I put it in my code 我把它放在我的代码中

driver.findElement(By.cssSelector(".add.u-yellow>span")).click();

... It works :) ... 有用 :)

I am not familiar with Javascript ,so I don't know why .add.u-yellow>span works. 我不熟悉Javascript,所以不知道为什么.add.u-yellow>span可以正常工作。 But FirePath seems to be a good tool to assists Webdriver developer :) 但是FirePath似乎是协助Webdriver开发人员的好工具:)

It could be a lot of things. 可能有很多事情。

  1. The element is not in the visible view port of the web browser 元素不在Web浏览器的可见视图端口中
  2. The page has not finished loading 页面尚未完成加载
  3. The element is behind another element 该元素在另一个元素后面

You can try to click on the parent element as someone else already suggested: 您可以尝试单击父元素,因为已经有人建议:

driver.findElement(By.cssSelector("li.add.u-yellow")).click();

Alternatively, you can bind the class of the span element: 另外,您可以绑定span元素的类:

driver.findElement(By.cssSelector("span[style='background-color: transparent;']")).click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM