简体   繁体   中英

Not able to click on a element in Selenium webdriver

I am writing Java code to automate one process but when I am trying to click on the button which has foloowing code

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

I am not able to click on it. I have tried all possible solutions but nothing is working for me. please help what should I write ?

I am writing code like this

 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();

This is my HTML page code

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

尝试这个 -

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

Hi please try like below

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

UPDATE :

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

Selenium can't find the element because your xpath

//a[@id='wa_global_kso_create']/ins

Does not point to a tag in your document.

The "/ins" at the end implies that your ins tag is a child of the anchor tag with id 'wa_global_kso_create' when in fact, the ins is a following-sibling.

You can remove the /ins from the end of the xpath to interact with the anchor tag

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

The code above should work fine, but if you need to interact with the ins tag instead of the anchor, you could use this xpath

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

Refer to http://www.w3schools.com/xsl/xpath_axes.asp for a list of Xpath axes.

FireBug or FirePath can be used to generate XPaths instead of writing them manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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