简体   繁体   中英

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

I am trying to find an element using Selenium. I tried both using x-path and class name , but both ways failed to click on the element.

Specifically, I am trying to find the new account link element, which is basically an onclick attribute.

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

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

     //driver.findElement(By.xpath(".//input[contains(@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 . 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"

     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:

  • xpath :

     driver.findElement(By.xpath("//div[@class='dashboard_menu_div dashboard-menu']//following::a[1]")).click(); 
  • 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. 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. There's nice tool to cleverly save all the element you need to interact with them later

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