-2

I am trying to solve the return of a promise in selenium, in order to get the text / variable behind it. The Client.onis 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

Bob Toby
  • 1
  • 1
  • @VLAZ, It might but i'm not sure how to implement that in my code :( – Bob Toby Apr 15 '23 at 16:38
  • What exactly is the problem that you're observing? In the code shown, which specific operation isn't doing what you expect? What were the values used in that operation? What is the observed result of that operation? What result was expected? – David Apr 15 '23 at 16:41
  • @David I added more context please tell me if you need more? – Bob Toby Apr 15 '23 at 16:45

1 Answers1

0

I managed to find the right way of implementing .then()

const Type = await driver.findElement(By.css('div.status')).getText().then(value =>
{
            console.log('Type:', value);
            return value
});
Bob Toby
  • 1
  • 1