如何使用Selenium验证页面源中存在的特定脚本?

时间:2018-08-01 07:44:44

标签: selenium

在页面源代码中,我具有以下脚本标记, 如何在硒中验证是否存在特定脚本?

public class BinarySearchTree {
    private BTNode root;

    public static void main(String[] args) {
        BinarySearchTree tree = new BinarySearchTree();

        tree.add(70);
        tree.add(20);
        tree.add(10);
        tree.add(27);
        tree.add(150);
        tree.add(115);
        tree.add(300);
        System.out.println(tree.toString());
    }

    @Override
    public String toString() {
        return root.toString();
    }

    public void add(int value) {
        root = add(root, value);
    }

    private BTNode add(BTNode node, int value) {
        if (node == null) {
            return new BTNode(value, null, null);
        }
        if (node.item > value)
            node.left = add(node.left, value);
        else if (node.item < value)
            node.right = add(node.right, value);
        return node;
    }

    static class BTNode {
        int item;
        BTNode left;
        BTNode right;

        BTNode(int i, BTNode l, BTNode r) {
            item = i;
            left = l;
            right = r;
        }

        @Override
        public String toString() { // return the pre-order traversal;
            StringBuilder stringBuilder = new StringBuilder();
            if (left != null) stringBuilder.append(left.toString());
            stringBuilder.append("[" + item + "]");
            if (right != null) stringBuilder.append(right.toString());
            return stringBuilder.toString();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您可以在脚本中搜索属性src。即按属性查找元素

driver.findElement(By.xpath("//script[@src='/core/assets/vendor/domready/ready.min.js?v=1.0.8']"))

OR

driver.findElement(By.xpath("//script[contains(@src,'/core/assets/vendor/domready/ready.min.js?v=1.0.8')]")

OR

driver.findElement(By.cssSelector("script[src='/core/assets/vendor/domready/ready.min.js?v=1.0.8']"))