Selenium WebDriver按部分类名称查找元素

时间:2014-11-20 21:02:19

标签: java firefox selenium selenium-webdriver

在我使用的框架中,我有以下元素:

<div class="x-grid3-cell-inner x-grid3-col-expRepCol">  New session from client IP 192.168.5.3 (ST=/CC=/C=) at VIP 192.168.5.2 Listener /Common/Tomcat (Reputation=Unknown)</div>

以及许多其他类似的元素。我试图通过部分名称文本找到此元素,并使用以下代码单击它:

String expectedText = "New session from client IP";
driver.findElement(By.className("div[class*='"+expectedText+"']")).click();

我也尝试过使用cssSelector:

String expectedText = "New session from client IP";
driver.findElement(By.cssSelector("div[class*='"+expectedText+"']")).click();

但WebDriver不断抛出异常,说明它无法找到该元素。关于可能出现什么问题的任何建议?

3 个答案:

答案 0 :(得分:2)

By.className正在寻找输入名称的课程。

By.cssSelector正在为您输入的选择器寻找匹配项。

您尝试的是将div的文字与class相匹配,这不起作用。

您可以尝试这样的事情:

driver.findElement(By.xpath("//div[contains(text(),'"+expectedText+"')]")).click();

答案 1 :(得分:0)

    public boolean replace(int index, Object newEntry) {
        boolean isSuccessful = true;
        Node nodeToReplace = getNode(index);
        getNode(index).data = newEntry;

        if(newEntry == nodeToReplace){
            isSuccessful = false;
        }
        return isSuccessful;
    } // end replace
    /** Task: Determines whether two lists are equal.
     * @param other object that contains the other linked list
     * @return true if two lists have the same length and all
     * entries are equal, or false if not */
    public boolean equals(Object other) {
        MyLinkedList aList = (MyLinkedList)other;
        boolean isEqual = true; // result of comparison of lists
        Node currentNode = firstNode;
        Node aListNode = firstNode;

        while ((currentNode != null)){
            if (currentNode.data == aListNode.data) {
                currentNode = currentNode.next;
                aListNode = aListNode.next;
            }
            else
                isEqual = false;
        }
        return isEqual;
    } // end equals
    public int getLength() {
        return length;
    }
    public boolean isEmpty() {
        return length == 0;
    }

    // @return an string with all entries in the list
    public String toString() {
        Node currentNode = firstNode;
        String s = new String();
        while (currentNode != null) {
            s += currentNode.data + " ";
            currentNode = currentNode.next;
        }
        return s;
    }
    private Node getNode(int index) {
        Node currentNode = firstNode;
        // traverse the list to locate the desired node
        for (int counter = 0; counter < index; counter++)
            currentNode = currentNode.next;
        return currentNode;
    } // end getNode
    private class Node {
        private Object data; // data portion
        private Node next; // link to next node

        private Node(Object dataPortion) {
            data = dataPortion;
            next = null;
        } // end constructor
        private Node(Object dataPortion, Node nextNode) {
            data = dataPortion;
            next = nextNode;
        } // end constructor
        private void setData(Object dataPortion) {
            data = dataPortion;
        } // end setData
        private Object getData() {
            return data;
        } // end getData
        private void setNextNode(Node nextNode) {
            next = nextNode;
        } // end setNextNode
        private Node getNextNode() {
            return next;
        } // end getNextNode
    } // end Node

以上2个是Yahoo搜索结果中的HTML元素。因此,如果我们想使用带有硒python的局部类名来获取这些元素,那么这里就是解决方案。

<div class="dd algo algo-sr Sr" data-937="5d1abd07c5a33">
<div class="dd algo algo-sr fst Sr" data-0ab="5d1abd837d907">

以同样的方式,我们可以获得与属性名称(例如类名,id等)部分匹配的任何元素。

find elements with css selector partial match on attribute values

答案 2 :(得分:-2)

我相信这会奏效:

driver.findElement(By.className("x-grid3-cell-inner x-grid3-col-expRepCol").click();