I am trying to solve the return of a promise in selenium, in order to get the text / variable behind it. The Client.on
is used because I am writing a discord bot to webscrap certain elements of a page.
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.content.startsWith('!FooBar')) {
await async function example() {
let driver = await new Builder().forBrowser('chrome').build()
try {
await driver.get(FooBar);
Title = await driver.getTitle();
console.log('Page title:', Title);
//No issue here
ABC = await driver.findElement(By.xpath('//div[contains(text(), "ABC")]/following-sibling::div')).getText();
console.log('THC Content:', THC);
//No issue here
imgSrc = await driver.findElement(By.css('img.logo')).getAttribute('src');
console.log('Image source:', imgSrc);
//here is where I encounter the problem
Type = await driver.findElement(By.css('div.status'))
console.log('Type:', Type.getText());
} catch (error) {
console.error(`Error occurred while scraping: ${error}`)
message.channel.send('Error during scraping');
}
}()
}
})
Currently the console.log is logging Promise { Pending }
for the line for the variable named Type
, when it should be logging the text within the div class status : FooBar
.
I have tried the solutions on other stack overflow pages but can't seem to adapt the solutions to my issue.
const hi = await driver.findElement(By.css('div.status'))
hi.then((response) =>
{
console.log(response.getText()
});
I can't seem to get the .then()
method to work