简体   繁体   中英

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. I need to click on a button, but the button has no ID or class attribute. 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. 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

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

or by text value, if necessary:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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