I am trying to throw an error on the console if the first link of bing search results is clicked. My code is working fine until the first link is opened, but I am not seeing any message on the console that first link is successfully clicked and when I make an intentional code change to catch error, catch is not throwing error. I am using node.js, selenium and edge webdriver. Please help me fix this.
const { Builder, By } = require('selenium-webdriver');
async function launchEdgeBing() {
let driver = await new Builder().forBrowser('MicrosoftEdge').build();
await driver.get('https://bing.com');
await driver.manage().window().maximize();
const element = await driver.findElement(By.id('sb_form_q'));
await driver.sleep(5000);
await element.sendKeys('latest news');
await element.submit();
await driver.sleep(5000);
try {
const clickFirstResult = await driver.findElement(
By.className('b_title')
);
clickFirstResult.click();
await driver.sleep(5000);
var title = await driver.getTitle();
console.log('Title is:', title);
} catch (e) {
if (e == null) {
console.log('First link opened successfully');
} else {
console.log('Error is:', e);
}
} finally {
await driver.quit();
}
}
launchEdgeBing();