【问题标题】:Unable to find the element in selenium which is having on-click attribute无法在 selenium 中找到具有点击属性的元素
【发布时间】:2017-10-15 10:41:31
【问题描述】:

我正在尝试使用 Selenium 查找元素。我尝试使用x-pathclass 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();

【问题讨论】:

  • 框架内有一个HTML表单标签,其中包含Type=hidden之类的属性,可能是这个原因我们找不到元素

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


【解决方案1】:
  1. 对于您尝试过的xpath 以下,应该是//a 而不是.//input

    //driver.findElement(By.xpath(".//input[contains(@onclick,'getDashboard().newAccount(event)')]")).click();
    
  2. 如果上面xpath,修改后还是不行,请添加一些调试代码,确保切换到正确的iframe。之后,您可以删除调试代码。对于调试代码,您可以在同一 iframe 中找到易于定位的元素,如标题:“欢迎 Muamalaty 门户”

    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");
    }
    

【讨论】:

  • 它不工作仍然遇到同样的异常。
  • 它无法正常工作,因为仍然出现相同的异常。即使是调试代码,我们也会遇到异常。无法找到元素:{"method":"css selector","selector":"div.dashboard_context_title"}
  • 您使用 for 循环并单击每个循环中的新帐户链接,我怀疑不是每个 iframe 都有新帐户链接。您应该直接切换到正确的 iframe,而不是使用 for 循环。
  • 感谢 Yong 切换到正确的框架后,它现在可以工作了。
【解决方案2】:

根据您提供的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();
    

【讨论】:

  • 两个选择器仍然出现相同的异常,例如无法定位元素:{"method":"css selector","selector":"div.dashboard_menu_div.dashboard-menu > a:nth -child(1)"}
【解决方案3】:

切换到正确的 iframe 后,它工作正常。 谢谢大家的支持。

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

driver.switchTo().frame(6); driver.findElement(By.cssSelector(".dashboard_menu_div_main")).click();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 1970-01-01
    • 2021-12-31
    • 2013-07-03
    • 2019-08-10
    相关资源
    最近更新 更多