It's more safety to use locator in playwright. You can try something like that.
const someFunc = async () => {
const table = await page.locator('#txnHistoryList > tbody')
const arrayData = []
const isVisibleTable = await table.isVisible()
if (isVisibleTable) {
const rows = table.locator('tr')
const rowsCount = await rows.count()
for (let i = 0; i < rowsCount; i += 1) {
const row = rows.nth(i)
const tdLocator = row.locator('td')
const tdLocatorCount = await tdLocator.count()
const arrays = {}
if (tdLocatorCount > 2) {
for (let j = 0; j < tdLocatorCount; j += 1) {
switch (j) {
case 1: {
const date = await tdLocator.nth(j).innerText()
arrays.date = date ? date.toString().trim() : ''
break
}
case 2: {
const transaction = await tdLocator.nth(j).innerText()
arrays.transaction = transaction ? transaction.toString().trim() : ''
break
}
case 4: {
const type = await tdLocator.nth(j).innerText()
arrays.type = type ? type.toString().trim() : ''
break
}
case 5: {
const paymentAmount = tdLocator.nth(j).innerText()
arrays.payment_amount = paymentAmount ? paymentAmount.toString().trim() : ''
break
}
case 6: {
const balance = tdLocator.nth(j).innerText()
arrays.balance = balance ? balance.toString().trim() : ''
break
}
default:
continue
}
arrayData.push(arrays)
}
}
}
}
return arrayData
}
Docs: https://playwright.dev/docs/api/class-locator
Answer from Stanislav Taran on Stack OverflowYou would just do:
await page.evaluate(() => alert("alert"))
But keep in mind, that alert's are blocking in the JavaScript browser world and dismissed automatically by Playwright. See here how to interact with them (dismiss e.g.)
for a custom function like "giveMeSomeKebab()" you should go for:
// javascript
await page.evaluate(() => window.giveMeSomeKebab)
// java
page.evaluate("window.giveMeSomeKebab");
for additional information: check this out
I've had that exact problem. It can be done with a little trickery, because page.evaluate also can accept a string.
There are several ways to do it, but I use a wrapper called evaluate, which accepts additional parameters to pass to the function that must be evaluated on the webkit side. You would use it like this:
page.open(url, function() {
var foo = 42;
evaluate(page, function(foo) {
// this code has now has access to foo
console.log(foo);
}, foo);
});
And here is the evaluate() function:
/*
* This function wraps WebPage.evaluate, and offers the possibility to pass
* parameters into the webpage function. The PhantomJS issue is here:
*
* http://code.google.com/p/phantomjs/issues/detail?id=132
*
* This is from comment #43.
*/
function evaluate(page, func) {
var args = [].slice.call(arguments, 2);
var fn = "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify(args) + ");}";
return page.evaluate(fn);
}
The change has been pushed and now you can use it as
page.open(url, function() {
var foo = 42;
page.evaluate( function(foo) {
// this code has now has access to foo
console.log(foo);
}, foo);
}
The push details are here: https://github.com/ariya/phantomjs/commit/81794f9096