简体   繁体   English

无法在 selenium 中找到具有点击属性的元素

[英]Unable to find the element in selenium which is having on-click attribute

I am trying to find an element using Selenium.我正在尝试使用 Selenium 查找元素。 I tried both using x-path and class name , but both ways failed to click on the element.我尝试使用x-pathclass name ,但两种方法都无法点击元素。

Specifically, I am trying to find the new account link element, which is basically an onclick attribute.具体来说,我试图找到新的帐户链接元素,它基本上是一个onclick属性。

<a onclick="getDashboard().newAccount(event)" href="#" class="dashboard_menu_div_main">

Below the full code.下面是完整代码。

<div class="dashboard" style="">
    <div class="dashboard_context">
        <div class="dashboard_context_title">Welcome Muamalaty Portal</div>In relation to the provision of Services and supply of Products by Etisalat Website Customer of Etisalat Website shall observe and be bound by Etisalat Conditions applicable to each.
        </div>
        <div class="dashboard_Body dashboard-content">
            <div class="dashboard_menu_div dashboard-menu">
                <ul>
                    <li>
                        <a onclick="getDashboard().newAccount(event)" href="#" class="dashboard_menu_div_main">
                            <div class="dashboard_menu_number">01</div>
                            <div class="dashboard_menu_img">
                                <img src="/cim/resources/images/produts/dashboard/new-account.png">
                            </div>
                            <div class="dashboard_menu_menu_fonts">New Account</div>
                        </a>
                    </li>
                    <li>
                        <a onclick="getDashboard().standAlone(event,'714857547');" href="#" class="dashboard_menu_div_main">
                            <div class="dashboard_menu_number">05</div>
                            <div class="dashboard_menu_img">
                                <img src="/cim/resources/images/produts/dashboard/standalone.png">
                            </div>
                            <div class="dashboard_menu_menu_fonts">Standalone Devices</div>
                        </a>
                    </li>
                    <li>
                        <a onclick="new PendingOrders().init({evt:event});" href="#" class="dashboard_menu_div_main">
                            <div class="dashboard_menu_number">08</div>
                            <div class="dashboard_menu_img">
                                <img src="/cim/resources/images/produts/dashboard/pending-orders.png">
                            </div>
                            <div class="dashboard_menu_menu_fonts">Pending Orders</div>
                        </a>
                    </li>
                    <li>
                        <a onclick="getDashboard().newPreOrder(event)" href="#" class="dashboard_menu_div_main">
                            <div class="dashboard_menu_number">16</div>
                            <div class="dashboard_menu_img">
                                <img src="/cim/resources/images/produts/dashboard/new-preorder.png">
                            </div>
                            <div class="dashboard_menu_menu_fonts">New PreOrder</div>
                        </a>
                    </li>
                    <li>
                        <a onclick="new Miscellaneous().init({evt:event});" href="#" class="dashboard_menu_div_main">
                            <div class="dashboard_menu_number">22</div>
                            <div class="dashboard_menu_img">
                                <img src="/cim/resources/images/produts/dashboard/miscellaneous.png">
                            </div>
                            <div class="dashboard_menu_menu_fonts">Miscellaneous services</div>
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

i tried below code since there is 7 iframe i tried to click on the location in all the frame and every time it is getting failed in all the frames.我尝试了下面的代码,因为有 7 个 iframe 我试图点击所有框架中的位置,每次它在所有框架中都失败了。

for(int i=0;i<=s1;i++) {
    try {
        driver.switchTo().frame(i);
        driver.findElement(By.xpath("./div[@class=\"dashboard\"]/div[2]/div[@class=\"dashboard_menu_div dashboard-menu\"]/ul/li/a[@onclick=\"getDashboard().newAccount(event)\"]@onclick")).click();

    }
    catch(Exception e) {
        System.out.println("failed "+i+" time");
    }
}

Another paths I've tried are:我尝试过的另一条路径是:

//driver.findElement(By.xpath(".//input[contains(@onclick,'getDashboard().newAccount(event)')]")).click();
//driver.findElement(By.cssSelector("//dashboard_menu_div > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1)")).click();
  1. for below xpath you tried, it should //a not .//input 对于您尝试过的xpath以下,它应该//a不是.//input

     //driver.findElement(By.xpath(".//input[contains(@onclick,'getDashboard().newAccount(event)')]")).click(); //driver.findElement(By.xpath(“ .// input [包含(@ onclick,'getDashboard()。newAccount(event)')]”)))。click();\n
  2. If above xpath , after changed still does not work, please add some debug code to make sure you switch into the correct iframe . 如果在xpath之上,更改后仍然不起作用,请添加一些调试代码,以确保切换到正确的iframe After that you can remove the debug code. 之后,您可以删除调试代码。 For debug code you can find the element which easy to be located in the same iframe , like the title: "Welcome Muamalaty Portal" 对于调试代码,您可以找到容易位于同一iframe的元素,例如标题:“ Welcome Muamalaty Portal”

     try { driver.switchTo().frame(i); //debug code begin String title = driver.findElement(By.cssSelector("div.dashboard_context_title")) .getText(); System.out.println("Title: " + title); //debug code end // click New Accont Link driver.findElement(By.cssSelector("a[onclick*='newAccount']")).click(); } catch(Exception e) { System.out.println("failed "+i+" time"); } 

As per the HTML you provided, we need to construct an unique cssSelector or a xpath to identify and click on the WebElement as follows: 根据您提供的HTML ,我们需要构造一个唯一的cssSelectorxpath来标识并单击WebElement ,如下所示:

  • xpath : xpath

     driver.findElement(By.xpath("//div[@class='dashboard_menu_div dashboard-menu']//following::a[1]")).click(); 
  • cssSelector : cssSelector

     driver.findElement(By.cssSelector("div.dashboard_menu_div.dashboard-menu > a:nth-child(1)")).click(); 

After Switching to the correct iframe its working fine. 切换到正确的iframe后,其工作正常。 Thanks for the support guys. 感谢你们的支持。

driver.switchTo().frame(6);
driver.findElement(By.xpath(".//*[@id='mainForm:productsList']/div[2]/div[3]/div[2]/div/ul/li[1]/a")).click();

or 要么

driver.switchTo().frame(6); driver.findElement(By.cssSelector(".dashboard_menu_div_main")).click();
    List<WebElement> elements = driver.findElementsByTagName("a");
    for (WebElement el : elements) {
        if (el.getAttribute("onclick").contains("newAccount(event)")){
            el.click();
            //or do what you need
        }

Just try to run thru all elements and find the exact one by attribute value. 只需尝试遍历所有元素并通过属性值找到确切的元素即可。

You can try out TestProject free automation platform. 您可以试用TestProject免费自动化平台。 There's nice tool to cleverly save all the element you need to interact with them later 有一个很好的工具可以巧妙地保存以后与之交互所需的所有元素

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

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