简体   繁体   English

Selenium Webdriverjs使用循环查找文本字符串

[英]Selenium Webdriverjs find text string using loop

Ok, this is getting annoying. 好的,这很烦人。 I've been trying to work this out for 4 days now and I am just stuck. 我已经尝试解决了4天了,但是我只是被卡住了。 I need to click on a button, but the button has no ID or class attribute. 我需要单击一个按钮,但是该按钮没有ID或类属性。 I think the only approach is to loop through all "Button" objects and find the one containing the text "Submit". 我认为唯一的方法是遍历所有“按钮”对象并找到包含文本“提交”的对象。 And then click on that element. 然后单击该元素。 But thanks to the Async nature, once you find the button object, you can't get that object. 但是由于异步特性,一旦找到按钮对象,就无法获得该对象。 I need some way to step back, or pass that object as part of the "innerHTML" function call. 我需要一些方法来退后,或将该对象作为“ innerHTML”函数调用的一部分传递。 Here is where I am at: 这是我的位置:

driver.findElements(webdriver.By.tagName('button')).then(function(webElements) 
{
    for(var i=0; i<webElements.length; i++)
    {
        driver.executeScript(function() {
            return webElements[i].innerHTML;
        }).then(function(innerHTML) {
             if(innerHTML == 'Submit')
                 webElements[i].click();   // webElements[i] is NULL
        });                
    }
});

You could locate the button via a CSS selector instead eg 您可以通过CSS选择器找到按钮,例如

driver.findElement(webdriver.By.css("input[type=submit]")).click();

or by text value, if necessary: 或按文本值(如有必要):

driver.findElement(webdriver.By.css("input[value='Submit']")).click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM