无法在selenium中找到具有on-click属性的元素

时间:2017-10-15 10:41:32

标签: java selenium selenium-webdriver selenium-firefoxdriver xpath-2.0

我正在尝试使用Selenium找到一个元素。我尝试使用x-path  和class name,但两种方式都未能点击元素。

具体来说,我正在尝试查找新的帐户链接元素,该元素基本上是onclick属性。

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

完整代码下方。

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

我试过下面的代码,因为有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");
    }
}

我尝试过的另一条道路是:

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

4 个答案:

答案 0 :(得分:0)

  1. 对于您尝试过的xpath以下,//a不是.//input

    //driver.findElement(By.xpath(".//input[contains(@onclick,'getDashboard().newAccount(event)')]")).click();
    
  2. 如果超过xpath,更改后仍然无效,请添加一些调试代码以确保切换到正确的iframe。之后,您可以删除调试代码。对于调试代码,您可以找到容易位于同一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");
    }
    

答案 1 :(得分:0)

根据您提供的HTML,我们需要构建一个唯一的cssSelectorxpath来识别并点击WebElement,如下所示:

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

答案 2 :(得分:0)

切换到正确的iframe后工作正常。 感谢支持人员。

driver.switchTo()框架(6)。 driver.findElement(By.xpath( “.//*[@ ID = '的MainForm:产品列表'] / DIV [2] / DIV [3] / DIV [2] / DIV / UL /利[1] /一种” ))点击();

driver.switchTo()框架(6)。 driver.findElement(By.cssSelector( “dashboard_menu_div_main。”))上单击();

答案 3 :(得分:0)

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

只需尝试遍历所有元素并通过属性值找到确切的元素即可。

您可以试用TestProject免费的自动化平台。有一个很好的工具可以巧妙地保存以后与之交互所需的所有元素