无法在selenium中单击网格上的过滤器

时间:2016-02-09 04:36:54

标签: java html css selenium xpath

我在下面给出了代码,我需要点击过滤器图标。请帮帮我

<thead class="k-grid-header" role="rowgroup">
    <tr role="row">
        <th class="k-header k-filterable k-with-icon" scope="col" data-title="User Name" data-index="0" data-field="UserName" data-role="columnsorter">
            <a class="k-grid-filter" href="javascript:void(0)" tabindex="-1">
                <span class="k-icon k-filter"/>
            </a>
           <a class="k-link" href="/Admin/AdminRoleGrid/Read?adminGrid-sort=UserName-asc">User Name</a>
        </th>

2 个答案:

答案 0 :(得分:0)

您需要先创建WebDriver。 请遵循以下代码,您的xpath将为//*[@class='k-icon k-filter']

    DesiredCapabilities dc=DesiredCapabilities.firefox();
    FirefoxProfile profile = new FirefoxProfile();
    dc.setCapability(FirefoxDriver.PROFILE, profile);
    WebDriver driver =  new FirefoxDriver(dc);
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

    driver.get("your-url");

    // code upto your filter icon

    // webelement of your filter icon
    WebElement webelement = driver.findElement(By.className("k-icon k-filter"));
    webelement.click();

您可以找到here有关selenium-webdriver的基础知识的更多信息。

答案 1 :(得分:0)

首先看起来你的定位器不好。在这里,您需要识别您的定位器。我写了一些xpath ..你可以逐个尝试......它可以如下: -

//a[@class='k-link' and contains(.,'User Name')]

OR

//a[@class='k-grid-filter']

OR

//span[class='k-icon k-filter']

OR

//th[@class='k-header k-filterable k-with-icon' and data-title='User Name']

您也可以尝试点击JavascriptExecutor

WebElement element= driver.findElement(By.xpath("Your Xpath"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

==========================

WebElement element1 = driver.findElement(By.xpath("//a[@class='k-grid-filter']"));
JavascriptExecutor executor1 = (JavascriptExecutor) oBrowser;
executor1.executeScript("arguments[0].click();", element1);

WebElement element = driver.findElement(By.xpath(".//*[@id='adminGrid']/div[1]/table/thead/tr/th[1]/a‌​[1]/span"));
JavascriptExecutor executor = (JavascriptExecutor) oBrowser;
executor.executeScript("arguments[0].click();", element);

希望它会对你有所帮助:)。