Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get text from a element ? #3051

Closed
ArvindReddyC opened this issue Aug 9, 2018 · 8 comments

Comments

@ArvindReddyC
Copy link

commented Aug 9, 2018

This might be a Obvious question but , Say i have this element
<span class="scrape">HelloPuppeteer</span>
I use
var a = page.$(".scrape");
Now , variable a holds a Element Handler returned from page.$(".scrape");
My question is how do i Now get this text ("HelloPuppeteer" ) using page.$(".scrape") is there any way to Scrape the text value of element using page.$(".scrape") ??

@vsemozhetbyt

This comment has been minimized.

Copy link
Contributor

commented Aug 9, 2018

You can try this:

    const element = await page.$(".scrape");
    const text = await page.evaluate(element => element.textContent, element);

or this:

    const element = await page.$(".scrape");
    const text = await (await element.getProperty('textContent')).jsonValue();
@ArvindReddyC

This comment has been minimized.

Copy link
Author

commented Aug 9, 2018

Now say I Have a input element ,
<input class="form-control" type="textbox" name="Month" value="6" id="april">
how do I send/set a value to this Element ? Using page.$("#april"); ?

@vsemozhetbyt

This comment has been minimized.

Copy link
Contributor

commented Aug 9, 2018

Maybe this way:

For DOM element properties

const element = await page.$("#april");
await page.evaluate(element => { element.value = 7; }, element);

For HTML attributes:

const element = await page.$("#april");
await page.evaluate(element => { element.setAttribute('value', 7); }, element);
@flyingsky

This comment has been minimized.

Copy link

commented Aug 10, 2018

why not page.type('#april', "7")?

@vsemozhetbyt

This comment has been minimized.

Copy link
Contributor

commented Aug 10, 2018

@flyingsky If only valueneeds to be changed, then why not) I meant a more general approach for any property or attribute.

@rostyk-kanafotskyy

This comment has been minimized.

Copy link

commented Feb 13, 2019

Now say I Have a input element ,
<input class="form-control" type="textbox" name="Month" value="6" id="april">
how do I send/set a value to this Element ? Using page.$("#april"); ?

Try this

await page.$eval("#april", el => el.value = '7');
@StebanDev

This comment has been minimized.

Copy link

commented Jul 12, 2019

You can try this:

    const element = await page.$(".scrape");
    const text = await page.evaluate(element => element.textContent, element);

or this:

    const element = await page.$(".scrape");
    const text = await (await element.getProperty('textContent')).jsonValue();

the shortest way for me:

const element = await page.$eval('.scrape', el => el.textContent);
@prprprus

This comment has been minimized.

Copy link

commented Jul 27, 2019

const elm = await page.$(".scrape");
const text = await page.evaluate(elm => elm.textContent, elm[0]);
console.log(text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.