How to copy text from Table using puppeteer in nodejs - node.js

I'm trying to extract the OTP text from HTML where it's inside a table row and there are no selector like div id or class. Only div style is present. How to copy text from that.
I'm using https://temp-mail.org/
Here is the XPATH of the OTP Field
/html/body/main/div[1]/div/div[3]/div[2]/div/div[1]/div/div[2]/div[3]/div[2]/div/table/tbody/tr/td/div[2]/table/tbody/tr/td/div/table/tbody/tr/td/table/tbody/tr[3]/td/div
Here is the Selector
body > main > div.container > div > div.col-sm-12.col-md-12.col-lg-12.col-xl-8 > div.tm-content > div > div.inboxWarpMain > div > div.inbox-data-content > div.inbox-data-content-intro > div:nth-child(13) > div > table > tbody > tr > td > div:nth-child(2) > table > tbody > tr > td > div > table > tbody > tr > td > table > tbody > tr:nth-child(3) > td > div
Code Structure Image Showing in Dev Tools

Always make sure that you don't violate the Terms of Use of the actually scraped service. Maybe you could achieve the desired result if you'd use their API? (https://rapidapi.com/Privatix/api/temp-mail)
If you are sure that you want to use browser automation and proceed with retrieveing the one-time password with puppeteer then: you can use page.$eval method to retrieve text content of any element with a valid selector.
Note: what you've already copied from devtools as the selector is actually a selector, it is not mandatory to include a CSS class or element id. It is totally fine (even if it is a bit redundant).
E.g.:
const selector = 'body > main > div.container > div > div.col-sm-12.col-md-12.col-lg-12.col-xl-8 > div.tm-content > div > div.inboxWarpMain > div > div.inbox-data-content > div.inbox-data-content-intro > div:nth-child(13) > div > table > tbody > tr > td > div:nth-child(2) > table > tbody > tr > td > div > table > tbody > tr > td > table > tbody > tr:nth-child(3) > td > div'
const text = await page.$eval(selector, el => el.innerText)
console.log(text)
Output:
233-552
Edit
In case there are more than one elements the selector would match, you can use document.querySelectorAll approaches like $$eval or $$ then select the element on the first index [0].
In this exact use case the $ is occupied by jQuery, so it conflicts with chrome api's $ shorthand for querySelector, see here:
Solutions:
const selector = 'body > main > div.container > div > div.col-sm-12.col-md-12.col-lg-12.col-xl-8 > div.tm-content > div > div.inboxWarpMain > div > div.inbox-data-content > div.inbox-data-content-intro > div:nth-child(13) > div > table > tbody > tr > td > div:nth-child(2) > table > tbody > tr > td > div > table > tbody > tr > td > table > tbody > tr:nth-child(3) > td > div'
await page.waitFor(10000) // waitForTimeout since pptr 5.3.0
try {
await page.waitForSelector(selector)
const [text] = await page.$$eval(selector, elements => elements.map(el => el.innerText))
console.log(text)
} catch (e) {
console.error(e)
}
// alternate solution with page.evaluate:
try {
const text = await page.evaluate(el => el.innerText, (await page.$$(selector))[0])
console.log(text)
} catch (e) {
console.error(e)
}

Related

how would I change the color on this? #agency_location > div > div.hl_controls > div.hl_controls--left > h1

how would I change the color on this? #agency_location > div > div.hl_controls > div.hl_controls--left > h1
how would I change the color on this? #agency_location > div > div.hl_controls > div.hl_controls--left > h1

Why can't I select an input box with a directly copied selector in Puppeteer?

Up until now I've been able to select input boxes to type in using Puppeteer however when I use waitForSelector, I Can't even find this element.
It's directly copied selector is
#root > div > main > div > main > div._pw._fj > div > div > input
The full html element is here. I'm beyond confused as to why it won't be found.
<input data-test-id="player_search:Input" type="text" role="search" class="_bw _eq _aj _nw _jx _db _ck _aw _ox _oy _oz _jw _w _pa" placeholder="Find a player" title="Find a player" value="">

I am trying to extract a text from an <a> tag that has a ::before selector using selenium/python

<ul class="ipc-inline-list ipc-inline-list--show-dividers ipc-inline-list--inline ipc-metadata-list-item__list-content baseAlt" role="presentation">
<li role="presentation" class="ipc-inline-list__item"><a class="ipc-metadata-list-item__list-content-item ipc-metadata-list-item__list-content-item--link" rel="" href="/name/nm9297923/?ref_=tt_ov_st">Ashley Ballard</a></li>
<li role="presentation" class="ipc-inline-list__item"><a class="ipc-metadata-list-item__list-content-item ipc-metadata-list-item__list-content-item--link" rel="" href="/name/nm8484653/?ref_=tt_ov_st">Elizabeth Cascarelli</a></li>
<li role="presentation" class="ipc-inline-list__item"><a class="ipc-metadata-list-item__list-content-item ipc-metadata-list-item__list-content-item--link" rel="" href="/name/nm8160673/?ref_=tt_ov_st">Jason Alan Cook</a></li>
</ul>
I tried XPATH,CSS selectors but nothing works
actorname1 = WebDriverWait(browser, 30).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,
'#__next > main > div > section.ipc-page-background.ipc-page-background--
base.TitlePage__StyledPageBackground-wzlr49-0.dDUGgO > section > div:nth-child(4) > section > section
> div.Hero__MediaContentContainer__Video-kvkd64-2.kmTkgc > div.Hero__ContentContainer-kvkd64-
10.eaUohq > div.Hero__MetaContainer__Video-kvkd64-4.kNqsIK >
div.PrincipalCredits__PrincipalCreditsPanelWideScreen-hdn81t-0.iGxbgr > ul > li.ipc-metadata-
list__item.ipc-metadata-list-item--link > div > ul > li:nth-child(1) > a'))).text
LOOKS UGLY IK
but I end up with AttributeError: 'list' object has no attribute 'text' as an error.
Then I found this answer Python - get text from CSS property “content” on a ::before pseudo element in Selenium?
but I couldn't understand how to edit such code in order to help my case (I know nothing in Javascript)
Assuming your selector is correct the result is a list, so you need to iterate over it
Get rid of the .text at the end of your element and iterate over it instead to grab the text values
for name in actorName1:
print(name.text)
actornames= driver.find_elements_by_xpath("//a[#class[contains(.,'ipc-metadata-list-item')]]")
for x in range(len(actornames)):
Actorname=actornames[x].text

I'm trying to log in to Disney shop. I've tried many different selectors but none of them seem to be working. Any help or tips?

Here is my code; it basically goes to the page, clicks the side navigation bar, clicks sign in and then waits for the email and password elements.
Problem is they aren't working.
await page.goto('https://www.shopdisney.com/');
await page.waitForSelector('body > div.page > header > nav > div > div.layout--maxWidth > div > div > div:nth-child(2) > div.float-left > button')
await page.click('body > div.page > header > nav > div > div.layout--maxWidth > div > div > div:nth-child(2) > div.float-left > button')
await page.waitForSelector('#sg-navbar-collapse > div > div > nav > div.close-menu.clearfix.d-inline-block.d-lg-none > div.user.usercontainer > div > button.signin-or-signupbutton')
await page.click('#sg-navbar-collapse > div > div > nav > div.close-menu.clearfix.d-inline-block.d-lg-none > div.user.usercontainer > div > button.signin-or-signupbutton')
await page.waitFor(5000)
let selector = '#did-ui-view > div > section > section > form > section > div:nth-child(1)'
await page.$eval(selector, elem => elem.click());
await page.waitForSelector('input[type="email"]')
await page.focus('input[type="email"]')
await page.keyboard.type('supreme#gmail.com', {delay: 100})
await page.waitForSelector('#did-ui-view > div > section > section > form > section > div:nth-child(2) > div > label > span.input-wrapper > input')
await page.focus('#did-ui-view > div > section > section > form > section > div:nth-child(2) > div > label > span.input-wrapper > input')
await page.keyboard.type('supreme123', {delay: 100})
await page.waitForSelector('#did-ui-view > div > section > section > form > section > div.btn-group.touch-print-btn-group-wrapper > button')
await page.click('#did-ui-view > div > section > section > form > section > div.btn-group.touch-print-btn-group-wrapper > button')
In my version of the site, this selector:
await page.waitForSelector('body > div.page > header > nav > div > div.layout--maxWidth > div > div > div:nth-child(2) > div.float-left > button')
Points to a element only enable in the mobile version, more precise, the hamburger menu.
Maybe you need to point to user.user__container selector.
What screen size use in puppeteer.browser?
Can you send the browser configuration? That because the screen size used by puppeteer change the available selectors, according the media queries of the site.

Can't click on element in modal using Nightwatch

I am writing some tests using Nightwatch, and I am at the very end of my test and I have 1 issue I can't figure out. I have a button to submit something, and then when they click submit, a modal pops up asking them if they are sure. I am trying to click "Yes, I am sure" but Nightwatch isn't finding the element. Below is my code.
Click on Submit Button (This works)
.getLocationInView('#objective-content-intro > div > div > div > div:nth-child(4) > div > div:nth-child(8) > div > div > a')
.assert.visible('#objective-content-intro > div > div > div > div:nth-child(4) > div > div:nth-child(8) > div > div > a')
.click('#objective-content-intro > div > div > div > div:nth-child(4) > div > div:nth-child(8) > div > div > a')
Click on "Yes, I'm sure" button (This doesn't work)
.getLocationInView('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button')
.assert.visible('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button')
.waitForElementPresent('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button', 3000, 'Click on \"Yes, submit it!\"')
.pause(3000)
.click('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button')
I just get an error saying it is unable to find the element.
Try this
.waitForElementPresent('#location > of > modal',20000, 'Some message here to show while running test')
.pause(1000)
.execute(function(){
document.querySelector('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button').click()
});
getLocationInView('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button')
.waitForElementPresent('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button', 3000,function() {
this.pause(3000)
.click('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button')
})
This also worked for me...
.acceptAlert()
https://v09.nightwatchjs.org/api/acceptAlert.html

Resources