Can anyone help me understand how looping in WebDriverJs works with promises?
Imagine you have the following html structure:
<div id="textelements">
<span onclick="spanClicked('Rock')">Rock</span>
<span onclick="spanClicked('Paper')">Paper</span>
<span onclick="spanClicked('Scissors')">Scissors</span>
</div>
Using WebDriverJs, I want to locate the span containing the text 'Scissors' and click on it.
The ideal scenario would involve adding appropriate identifiers in the source HTML. However, if that is not an option, what would the WebDriverJs code look like for the given HTML structure?
I attempted the following approach:
function clickElementWithText(driver, textValue) {
driver.findElements(webdriver.By.css('#textelements span')).then(function(spans) {
for (var i = 0; i < spans.length; i++) {
var matched = spans[i].getText().then(function(text) {
console.log('Text value is: ' + text);
return text === textValue;
});
console.log(matched);
if (matched === true) {
console.log('clicking!');
spans[i].click();
return;
}
}
});
}
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
driver.get('http://webdriverjsdemo.github.io/');
clickElementWithText(driver, 'Scissors');
The issue arises when the variable matched does not evaluate to true
as expected, even though it was supposed to be set as true.
Any insights into what might be causing this behavior?