As a newcomer to Selenium Webdriver (Js), I am eager to learn the basics but have hit a roadblock with a specific example:
My current challenge is automating a Google search for "Diablo 2 Resurrected days until release" and then displaying the remaining days in the console from the search results.
The issue I'm facing revolves around displaying the days left in my console. It seems like the element I'm trying to retrieve using .getText() isn't available when the webdriver attempts to locate it. I've tried using driver.sleep() and implicitlyWait without success. Both xpath and css selectors didn't work for me either. Here's the snippet of my code:
const {Builder, By, Key, until, WebDriver} = require("selenium-webdriver");
const { elementIsVisible } = require("selenium-webdriver/lib/until");
require("chromedriver");
let driver = new Builder().forBrowser("chrome").build();
driver.get("https://google.com");
// Handling cookie accept button
driver.findElement(By.css("#L2AGLb > div")).click();
driver.findElement(By.css('body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form > div:nth-child(1) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input')).sendKeys('Diablo 2 Resurrected days until release');
driver.findElement(By.xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]')).click();
driver.findElement(By.xpath('/html/body/div[7]/div/div[9]/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div[1]/div[1]/div[1]/div/div[2]/div/div[1]')).getText().then(txt => {
console.log("Do D2R relias: " + txt)
}).catch(err => {
console.log('error!', err);
});
Encountered Error Message:
error! NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[7]/div/div[9]/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div[1]/div[1]/div[1]/div/div[2]/div/div[1]"}
Edit: Issue Solved! Below is the updated working code
const {Builder, By, Key, until, WebDriver} = require("selenium-webdriver");
require("chromedriver");
let driver = new Builder().forBrowser("chrome").build();
async function howManyDaysTillDiablo2ResurrectedRelease() {
await driver.get("https://google.com");
await driver.findElement(By.css("#L2AGLb > div")).click();
await driver.findElement(By.css('body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form > div:nth-child(1) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input')).sendKeys('Diablo 2 Resurrected days until release');
await driver.findElement(By.xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]')).click();
let ele = await driver.wait(until.elementLocated(By.css("div[data-attrid*='calculate:how_many_days_away'][role='heading'] div")), 10000);
let foo = await ele.getText();
await driver.findElement(By.css("div[data-attrid*='calculate:how_many_days_away'][role='heading'] div")).getText().then(foo => {
console.log("Do D2R relias: " + foo)
}).catch(err => {
console.log('error!', err);
})};
howManyDaysTillDiablo2ResurrectedRelease();