Selecting an input element and filling it with a certain value is straight forward
Element
<input class='foo' type="test" name="userName" placeholder="Enter Your Name..." />
Code with click() function
(async () => {
const browser = await puppeteer.launch({
headless: false,
args: [
'--incognito'
]
});
const page = await browser.newPage()
await page.goto('...')
await page.type('input[name=userName]', 'test#test.com')
....
})();
I use input[name=userName] to select the wanted HTML Element.
However, when I try this with <span /> and the text attribute, it's not working.
Element
<span text="foo" id class="bar" />
Code with click() Function
(async () => {
const browser = await puppeteer.launch({
headless: false,
args: [
'--incognito'
]
});
const page = await browser.newPage()
await page.goto('...')
await page.type('input[name=userName]', 'test#test.com')
....
await page.waitForNavigation({ waitUntil: load })
await page.click('span[text=foo]'); // <-- not working
})();
Is there a straight forward way to select other elements than input by their values attached to their attributes?
The load in await page.waitForNavigation({ waitUntil: load }) was not set into '' which caused the <span /> not to be there, after the button to log in was clicked...
Not familiar with Puppeteer. But in Vanilla JavaScript you could do:
document.querySelector('span[text="foo"]');
After a little digging, looks like the below is how you could do it in Puppeteer.
const spanText = await page.evaluate(() => document.querySelector('span[text="foo"]'));
Related
i need to get the name of the player, his hp, and armor from site, when i try to get the name - everything is fine, but when i add his hp there - everything breaks
Help!
HTML -
<div class="marker__label">
<img src="https://vanilla.zalupa.online/tiles/faces/32x32/21kranel.png" class="player__icon" width="32" height="32">
<span class="player__name">21kranel</span>
<meter class="player__health" max="20" value="20"></meter>
<meter class="player__armor" max="20" value="20"></meter>
</div>
Javascript -
const url = "https://vanilla.zalupa.online/#world;flat;"
const browser = await puppeteer.launch({
headless:false
})
const page = await browser.newPage()
await page.goto(url,{
networkIdle2Timeout: 5000,
waitUntil: 'networkidle2',
timeout: 3000000});
const getData = await page.evaluate(() =>
Array.from(document.querySelectorAll('.player__name,.player__health,.player__armour'))
.map(player => ({
name: player.innerHTML,
health: player.value,
armour: player.value,
}))
)
const username = JSON.stringify(getData,null,2)
console.log(username)
await page.close()
await browser.close()
Tried to do everything in different functions but I need everything in one element
I'm trying to open a new window page from a button in Puppeteer.
An example given: I'm logging to a website and the moment I click the button for the login a new fresh window page will pop-up, redirecting to the site the button is meant to be going. How can I do it?
You can do that by simply pressing Shift button while doing page.click
And to catch the newly opened window you can use waitForTarget.
const puppeteer = require('puppeteer')
;(async () => {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
})
const context = browser.defaultBrowserContext()
const page = (await context.pages())[0]
await page.goto('https://www.amazon.com/gp/product/B093GQSVPX/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1', {waitUntil: 'load'})
await page.waitForSelector('a[title="Add to List"]', {visible: true})
await page.keyboard.down('Shift')
await page.click('a[title="Add to List"]')
await page.keyboard.up('Shift')
const popup = await browser.waitForTarget(
(target) => target.url().includes('www.amazon.com/ap/signin')
)
const popupPage = await popup.page()
await popupPage.waitForSelector('a.a-link-expander[role="button"]')
await popupPage.click('a.a-link-expander[role="button"]')
await popupPage.click('input#continue[type="submit"]')
await browser.close()
})()
I'm making a Facebook Messenger 'Bot'. I need to evaluate the last message sent to the bot, and respond to it. Listening to the new messages was straightforward, but how can i respond? I tried :newMessage.textContent = 'Hi' and then submit. but that doesn't work. And of course page.type()
won't work inside page.evaluate()
Please Help
Not sure if this is the simplest solution but here you go)
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch({ headless: false, defaultViewport: null });
const html = `
<!doctype html>
<html>
<head><meta charset='UTF-8'><title>Test</title></head>
<body>
<input>
</body>
</html>`;
try {
const [page] = await browser.pages();
await page.goto(`data:text/html,${html}`);
await page.exposeFunction('puppeteerType', async (text) => {
await page.type('input', text);
});
await page.evaluate(async () => {
await window.puppeteerType('Hi from page.type().');
});
} catch (err) { console.error(err); }
I use the following blog to use the playright
login and I need something similar to use for my app, when I use the headless:flase
I see it opens the UI with the user password in however, it doesnt click on the logon button, I adopt the code I try with the following , am I missing something?
(async () => {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://app.com');
await page.fill('input[type="text"]', 'user#test.com');
await page.fill('input[type="password"]', 'Abcd1234!');
// page.click('div[data-testid="LoginForm_Login_Button"]');
page.click('div[id="logOnFormSubmit"]');
}
)();
You are currently using
page.click('div[id="logOnFormSubmit"]');
There is no div in your given code example with that ID, but instead there is a button. You'd need to change that line to reflect this. The final code would look like below.
(async () => {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://app.com');
await page.fill('input[type="text"]', 'user#test.com');
await page.fill('input[type="password"]', 'Abcd1234!');
// page.click('div[data-testid="LoginForm_Login_Button"]');
page.click('button[id="logOnFormSubmit"]');
}
)();
Hello I try to make a screenshot with Playwright but I have cookie EU law popup on my screenshots. How can I remove them ?
Here is my browser parameters.
const browser = await playwright.firefox.launch({
headless: true,
firefoxUserPrefs: {
"network.cookie.cookieBehavior": 2
}
});
But it don't work.
Thank for your help.
Use the playwright API to click the element. I'm using the text selector in the example below, but you can use any selector.
const { webkit } = require('playwright');
(async() => {
const browser = await webkit.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://npmjs.com');
await page.click('text=Accept');
await page.screenshot({ path: 'screenshot.png' });
await browser.close();
})();