简体   繁体   English

无法单击 Selenium webdriver 中的元素

[英]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我正在编写 Java 代码来自动化一个过程,但是当我尝试单击具有以下代码的按钮时

<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这是我的 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>

尝试这个 -

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 Selenium 找不到元素,因为您的 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.最后的“/ins”暗示你的ins标签是id为'wa_global_kso_create'的锚标签的标签,而实际上,ins是一个后续兄弟姐妹。

You can remove the /ins from the end of the xpath to interact with the anchor tag您可以从 xpath 的末尾删除 /ins 以与锚标记交互

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上面的代码应该可以正常工作,但是如果您需要与 ins 标记而不是锚进行交互,则可以使用此 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.有关 Xpath 轴的列表,请参阅http://www.w3schools.com/xsl/xpath_axes.asp

FireBug or FirePath can be used to generate XPaths instead of writing them manually. FireBugFirePath可用于生成 XPath,而不是手动编写它们。

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

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