无法单击Selenium webdriver中的元素

时间:2016-05-02 11:27:05

标签: java selenium selenium-webdriver

我正在编写Java代码来自动执行一个进程,但是当我尝试单击具有以下代码的按钮时

<em class="k-ico-new-post"></em>
<ins class="visuallyHidden">Create Post</ins>

我无法点击它。我已经尝试了所有可能的解决方案,但没有什么对我有用。请帮助我写什么?

我正在编写像这样的代码

 new WebDriverWait(driver, 20)
    .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@id='wa_global_kso_create']/ins")));

driver.findElement(By.xpath("//a[@id='wa_global_kso_create']/ins")).click();

new WebDriverWait(driver, 20)
    .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("em.k-ico-new-post")));

driver.findElement(By.cssSelector("em.k-ico-new-post")).click();

这是我的HTML页面代码

<div class="page-container">
    <div class="titleBar" role="banner">
        <h1 class="visuallyHidden">Welcome To Page</h1>
        <ul id="skip" class="skip visuallyHidden">
        <div class="navbar navbar-fixed-top navbar-inverse ng-scope" ng-controller="PlaceholderCtlr">
            <div class="headerSplashStrip loaded"></div>
            <div class="navbar-inner mega-menu">
                <div class="container-fluid">
                    <a id="js_knome-brand" class="brand" accesskey="1" href="/">
                    <img class="visuallyHidden" src="print-5d795ba4408e17b69656ca7e2ac042a6.png" aria-hidden="true" alt="Page Logo">
                    <div class="Page-credits">
                        <ul class="nav">
                            <span class="nav-search rel">
                            <ul id="js_accessible_notify_converse" class="nav pull-right ng-scope" role="region" ng-controller="NotificationMessagesCtlr" aria-label="utility">
                            <li id="js_messages_container" class="dropdown">
                            <li id="js_notifications_container" class="dropdown">`enter code here`
                            <li class="divider-vertical visible-desktop"></li>
                            <li class="dropdown visible-desktop">
                            <li class="divider-vertical"></li>
                            <li class="nav-post">
                                <a id="wa_global_kso_create" class="page-tooltipped js_create_new_post primary" href="#" data-placement="bottom" data-original-title="Create new post">
                                <em class="k-ico-new-post"></em>
                                <ins class="visuallyHidden">Create Post</ins>
                                </a>
                            </li>
                            <li class="divider-vertical"></li>
                            <li class="headerProfilethumb dropdown js_user_profile">
                        </ul>
                    </div>
                </div>
            </div>
            <div id="page-ajax-loader-bar"></div>
        </div>
    </div>
</div>

3 个答案:

答案 0 :(得分:1)

试试这个 -

driver.findElement(By.xpath("//*[contains(text(),'Button Name')]")).click();

答案 1 :(得分:0)

嗨,请尝试如下

Thread.sleep(1000);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("$('#wa_global_kso_create .visuallyHidden').click();");

更新:

Thread.sleep(1000);
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("$('#js_knome-brand .page-tooltipped').click();");

答案 2 :(得分:0)

由于您的xpath

,Selenium无法找到该元素
//a[@id='wa_global_kso_create']/ins

不指向文档中的标记。

&#34; / ins&#34;最后暗示您的ins标签是锚标签的,其ID为&#39; wa_global_kso_create&#39;事实上,ins是一个跟随兄弟。

您可以从xpath的末尾删除/ ins以与锚标记交互

driver.findElement(By.xpath("//a[@id='wa_global_kso_create']")).click();

上面的代码应该可以正常工作,但如果你需要与ins标签而不是锚进行交互,你可以使用这个xpath

//a[@id='wa_global_kso_create']/following-sibling::ins[1]

有关Xpath轴的列表,请参阅http://www.w3schools.com/xsl/xpath_axes.asp

FireBugFirePath可用于生成XPath,而不是手动编写。