Successfully locating the element, I am able to retrieve the text within the container. However, when attempting to fetch the titles of products by iterating through the array of WebElements inside the container, the .findElement() consistently throws an error:
Note that #product_container is dynamically generated
<div id="product_container">
<div class="product"><div class="wrapper"><span class="product_title">Title 1</span></div></div>
<div class="product"><div class="wrapper"><span class="product_title">Title 2</span></div></div>
<div class="product"><div class="wrapper"><span class="product_title">Title 3</span></div></div>
<div class="product"><div class="wrapper"><span class="product_title">Title 4</span></div></div>
</div>
const {Builder, By, Key, until} = require('selenium-webdriver');
(async function example() {
let driver = await new Builder().forBrowser('chrome').build();
let container = By.css('#product_container');
try {
await driver.get('https://example.com/search/?q=mySearchText');
await driver.wait(until.elementLocated(container), 10000); // <-- Success
const result = await (await driver.findElement(container)).getText() // <-- Success
console.log(result) // <-- Success
const items = await (await driver.findElement(container)).findElements(By.css('.product')) // <-- Success
items.forEach(async (item, i)=>{
// Find the title of each product
item
.findElement(By.css('.product_title'))
.then(()=>{
console.log('success')
})
.catch(err=>{
console.log('failed', err) // <-- Always rejected to here
});
})
} finally {
await driver.quit();
}
})();
The error message encountered
failed Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:56016 at ClientRequest. (/./node_modules/selenium-webdriver/http/index.js:262:15) at ClientRequest.emit (events.js:315:20) at Socket.socketErrorListener (_http_client.js:426:9) at Socket.emit (events.js:315:20) at emitErrorNT (internal/streams/destroy.js:92:8) at emitErrorAndCloseNT (internal/streams/destroy.js:60:3) at processTicksAndRejections (internal/process/task_queues.js:84:21)