Selenium,findelement,整个<li>

时间:2016-05-18 09:48:28

标签: jquery html css selenium xpath

我需要从包含单词&#34; Produktgewicht&#34;

的网页中找到元素

这是用于查找元素的代码:

String Weight=wd.findElement(By.xpath("//*[contains(text(), 'Produktgewicht')]")).getText();

这是网页的源代码:

<li>
    <b>
        Produktgewicht inkl. Verpackung:
    </b>
     6 Kg
</li>

我的代码的输出是&#34; Produktgewicht inkl。 VERPACKUNG:&#34;

但我想要整个<li>

这是我想要的输出&#34; Produktgewicht inkl。 Verpackung:6公斤&#34;

我甚至试过

String Weight=wd.findElement(By.xpath("//*[contains(text(), 'Produktgewicht')]/following::li")).getText();

但它给了我下一个<li>

请帮帮我...... 在此先感谢:)

3 个答案:

答案 0 :(得分:0)

您可以尝试这种方式:

//li[contains(., 'Produktgewicht')]

上述内容应返回包含文字&#39; Produktgewicht&#39;的<li>,无论文本嵌套在<li>中多深。将其与*[contains(text(), 'Produktgewicht')]进行对比,/following::li仅测试第一个直接子文本节点。

您尝试过的

<li>无法正常工作,因为<b>未跟随之前匹配的<b>元素,而是//*[contains(text(), 'Produktgewicht')]/parent::li 的父级代替:

 var elfinder = $('#elfinder').elfinder({
            url: '<?= $connector; ?>',
            soundPath: '<?= site_url('assets/plugins/elFinder/sounds/rm.wav'); ?>',
            height: 700,
            lang: 'zh_TW',
            uiOptions: {
                // toolbar configuration
                toolbar: [
                    ['back', 'forward'],
                    ['reload'],
                    ['mkdir', 'upload'],
                    ['copy', 'cut', 'paste', 'rm'],
                    ['rename'],
                    ['view', 'sort']
                ]
            },
            contextmenu: {
                navbar: ['open', '|', 'copy', 'cut', 'paste', 'duplicate', '|', 'rm', '|', 'info'],
                cwd: ['reload', 'back', '|', 'upload', 'mkdir', 'paste', '|', 'info'],
                files: [
                    'open', 'quicklook', 'sharefolder', '|', 'download', '|', 'copy', 'cut', 'paste', 'rm', '|', 'rename', '|', 'info'
                ]
            },
            ui: ['toolbar', 'tree', 'stat'],
            handlers: {
                add: function (e) {
                },
                upload: function (e, instance) {
                    alert("test1");
                    //alert("test2");
                    //return false;
                    //console.log(event.data);
                    //console.log(event.data.selected); // selected files hashes list
                }
            }
        });

答案 1 :(得分:0)

试试这个:

{{1}}

答案 2 :(得分:0)

Xpath可以相当简单:

//li[b[contains(.,'Produktgewicht')]]/text()

这应该在给定的 li元素中获取整个文本。您还可以通过添加xpath后面的索引值来进行其他选择以标记 Produktgewicht 6 Kg :f。:

//li[b[contains(.,'Produktgewicht')]]/text()[0]
//li[b[contains(.,'Produktgewicht')]]/text()[1]

首先将标记 Produktgewicht inkl。 Verpackung:,第二个将标记 6 Kg 文本值