如何使用xpath定位动态元素(结束 - 函数不起作用)

时间:2016-03-17 06:57:56

标签: selenium xpath

我使用了end-with function as (By.xpath( “//输入处结束-与(@id, '_ PHONE $']”));

但它没有用

5 个答案:

答案 0 :(得分:3)

ends-with函数是XPath 2.0的一部分,但浏览器通常只支持1.0。

因此,如果您严格要获取以特定模式结尾的所有元素,则可以获取包含该模式的所有元素(使用contains()函数),然后严格检查{{的后缀1}}(使用Java .getAttribute("id")方法获取.endsWith())的属性值。

或者

您可以使用id函数,string-length函数和等于获取此XPath:

substring

答案 1 :(得分:1)

您也可以使用以下XPath: -

//input[contains(@id,'PHONE$1')]

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

答案 2 :(得分:0)

您的ID以_PHONE $ 1而非_PHONE $结尾。请注意,最后有一个1

(By.xpath("//input[ends-with(@id,'_PHONE$1']"));

如果您仍然不想匹配该1使用contains

By.xpath("//input[contains(@id,'_PHONE$1']"));

答案 3 :(得分:0)

var name = 'SuperAccounts';
//*** Configure grunt
console.log(jasmine);
grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    jshint: jshint,
    durandal: durandal,
    jasmine: {
        coverage : {

            src: [
               '../../../Test.UnitTest.JS/UnitTests/' + name + '/**/*.js'
            ],
            options: {
                template: require('grunt-template-jasmine-istanbul'),
                specs: [
                        //'../Test.UnitTest.JS/UnitTests/' + name + '/common/*.js',
                        //'../Test.UnitTest.JS/UnitTests/' + name + '/testdata',
                        '../Test.UnitTest.JS/UnitTests/' + name + '/**/*.js'
                ],
                helpers: [
                        'Assets/scripts/ato/helperscript.js'

                        //'Legacy/Shared/common/constants.js'
                ],
                vendor: jasmine.SuperAccounts,
                templateOptions: {
                    //files: 'src/assets/js/app/**/*.js',
                    coverage: 'bin/coverage/coverage.json',
                    report: 'bin/coverage',
                    thresholds: {
                        lines: 75,
                        statements: 75,
                        branches: 75,
                        functions: 90
                    },
                    replace: false,
                    template: require('grunt-template-jasmine-requirejs'),
                    templateOptions: {
                        requireConfig: {
                            baseUrl: 'SinglePageApplications/' + name,
                            //waitSeconds: 30,
                            paths: mixIn({
                                'knockout-editables': '../../Assets/scripts/vendor/ko.editables-0.9.0',
                                'knockout-validation': '../../Assets/scripts/vendor/knockout.validation-1.0.2',
                                'bignumber': '../../Assets/scripts/vendor/bignumber-1.4.1',
                                'testutils': '../../../Test.UnitTest.JS/Utils',
                                'shared': '../../Legacy/Shared',
                                'testdata': '../../../Test.UnitTest.JS/UnitTests/' + name + '/testdata'
                            }, addConfigurationPaths(config.SuperAccounts))

                        }
                    },
                    helpers: [
                        'Assets/scripts/ato/helperscript.js'

                        //'Legacy/Shared/common/constants.js'
                    ],
                    specs: [
                        //'../Test.UnitTest.JS/UnitTests/' + name + '/common/*.js',
                        //'../Test.UnitTest.JS/UnitTests/' + name + '/testdata',
                        '../Test.UnitTest.JS/UnitTests/' + name + '/**/*.js'
                    ],
                    //junit: {
                    //    path: 'build/junit/' + name + '/'
                    //},
                    timeout: 100000,
                    vendor: jasmine.SuperAccounts
                }
            }
            ,
            //specs : 'src/test/js/unit-headless.html',

            phantomjs: {
                'ignore-ssl-errors': true
            },


        }

    },
    uglify: uglify,
    cssmin: cssmin,
    autoprefixer: {
        options: {
            diff: true,
            map: true,
            browsers: ['last 2 versions', 'ie 8', 'ie 9']
        },
        multiple_files: {
            expand: true,
            flatten: true,
            src: 'ui/styles/ato/**/*.css',
            dest: 'ui/styles/ato/autoprefix'
        },
    },
    compress: {
        main: {
            options: {
                archive: staticsZip,
                level: 9
            },
            files: [
                { src: ['ui/**'], dest: '/', filter: 'isFile' }
            ]
    }
    }
});

//*** Loading plugins
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-durandal');
grunt.loadNpmTasks('grunt-contrib-jasmine');

答案 4 :(得分:0)

使用包含方法

By.xpath("//input[contains(text(), '_PHONE$']");

或使用等待显式方法