使用 PuppeteerSharp,我试图获取元素的文本。
ElementHandle elementHandle = await page.XPathAsync("//html/body/div[1]/section/div/section/h2")[0];
Run Code Online (Sandbox Code Playgroud)
现在我有了元素句柄,我该如何从中获取文本呢?我没有看到任何明显的方法。我本来期望 TextAsync 或类似的东西,但我没有看到它。
使用 PuppeteerSharp 5.0。
例如,我正在测试一个搜索页面,它将显示结果编号.text > span:nth-child(1)。
但是,如果没有任何结果,则仅显示text="nothing"或.text > span:nth-child(1)不存在。
那么我怎样才能同时满足这两个条件呢?
我正在使用 Jest-Puppeteer 端对端测试 Rails 应用程序。在这些测试之前,我想运行一些种子并在 DRY 工作之前告诉服务器在每次测试之前转到某个 URL。
// imports
describe("user can", () => {
// here are some constants
let page;
beforeAll(async () => {
await executeSeed(const1);
await executeSeed(const2);
await executeSeed(const3);
page = await newPrivatePage();
await login(page);
});
beforeEach(async () => {
await page.goto(baseUrl("/some-route"));
});
describe("view some great files", () => {
});
Run Code Online (Sandbox Code Playgroud)
我希望种子首先被执行,因为这是 beforeAll 并且如果第一个测试完成 beforeEach 将再次完成,但我在 jest 的文档中找不到它(https://jestjs.io/docs /en/api#beforeallfn-timeout )
我正在使用 puppeteer 进行网页抓取,我需要设置一个请求拦截来读取从浏览器下载的文件,而不实际下载它,因为下载读取然后删除它需要大量资源。
我已经识别了该请求,但找不到读取它的方法
await pages[0].setRequestInterception(true);
pages[0].on('request', request => {
if (request.resourceType() === 'font' || request.resourceType() === 'stylesheet' || request.resourceType() === 'image') {
request.abort();
} else {
request.continue();
}
});
Run Code Online (Sandbox Code Playgroud) 我以下列方式使用exposeFunction-command:
await this.page.exposeFunction('foo', function(){ return 'bar'; });
Run Code Online (Sandbox Code Playgroud)
这按预期工作,并为我提供了 window.foo 函数。
如果我再次调用此代码,则会出现以下错误:
Error: Failed to add page binding with name foo: window['foo'] already exists!
Run Code Online (Sandbox Code Playgroud)
使用 page.goto() 导航时,此错误甚至仍然存在。
有没有办法解除exposeFunction()公开的函数的绑定?
我正在尝试使用 puppeteer 创建 PDF。创建PDF时设置视口根本没有效果。但是,视口设置确实适用于屏幕截图。过去似乎在 github 上发现了一些问题,但它们显然已被关闭。传入 defaultViewport: null 应该是解决方案。
这是我的代码:
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: null,
executablePath: await chromium.executablePath,
ignoreHTTPSErrors: true,
headless: true,
});
let page = await browser.newPage();
// Set viewport
await page.setViewport({width: 1440, height: 900, deviceScaleFactor: 2});
// Generate pdf
const doc = await page.pdf(options);
Run Code Online (Sandbox Code Playgroud)
我还尝试在启动时传递视口设置。
谢谢!
javascript webautomation node.js google-chrome-headless puppeteer
我想使用 Playwright for Python 一次打开多个 url。但我正在努力弄清楚如何做。这是来自异步文档:
async def main():
async with async_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = await browser_type.launch()
page = await browser.newPage()
await page.goto("https://scrapingant.com/")
await page.screenshot(path=f"scrapingant-{browser_type.name}.png")
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)
这将按顺序打开每个 browser_type。如果我想并行进行,我该怎么做?如果我想对网址列表做类似的事情,我该怎么做?
我尝试这样做:
urls = [
"https://scrapethissite.com/pages/ajax-javascript/#2015",
"https://scrapethissite.com/pages/ajax-javascript/#2014",
]
async def main(url):
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False)
page = await browser.newPage()
await page.goto(url)
await browser.close()
async def go_to_url():
tasks = [main(url) for url in urls]
await asyncio.wait(tasks)
go_to_url()
Run Code Online (Sandbox Code Playgroud)
但这给了我以下错误:
92: RuntimeWarning: …Run Code Online (Sandbox Code Playgroud) python webautomation web-scraping playwright playwright-python
我正在使用 playwright.js 为https://target.com编写脚本,并且在您提交运输信息的页面上,如果您之前已完成结帐流程,它将提供使用已保存地址的选项目标帐户。
我想每次运行脚本时都输入新的运输信息,所以我必须让编剧在页面上点击删除,然后输入运输信息。
下面显示的函数用于单击删除,但随后超时if (await page.$$("text='Delete'") != [])而不是执行else该函数的一部分。
我怎样才能重写这个函数,让它简单地检查元素(选择器:)是否text='Delete'存在,如果存在则单击它,如果不存在则执行函数的填充部分?
async function deliveryAddress() {
if (await page.$$("text='Delete'") != []) {
await page.click("text='Delete'", {force:true})
await deliveryAddress()
} else {
await page.focus('input#full_name')
await page.type('input#full_name', fullName, {delay: delayms});
await page.focus('input#address_line1')
await page.type('input#address_line1', address, {delay: delayms});
await page.focus('input#zip_code')
await page.type('input#zip_code', zipCode, {delay: delayms});
await page.focus('input#mobile')
await page.type('input#mobile', phoneNumber, {delay: delayms});
await page.click("text='Save & continue'", {force:true})
}
}
Run Code Online (Sandbox Code Playgroud) 我需要创建一个 PDF 缓冲区并将其保存到数据库中。我将完整的 DOM 传递给 puppeteer,其中大部分工作得很好。当我打开创建的 PDF 缓冲区时,将应用引导样式,我会得到一个漂亮的 PDF。
但是,字体很棒的图标不会显示。我只有两个 CSS 文件:framework.css(使用 SASS 创建并包含自定义样式、引导样式和 font-awesome)和 print-media(包含打印媒体 css 以隐藏或显示导航等内容)。这是我创建 PDF 缓冲区的代码:
const browser = await puppeteer.launch({
args: ['--disable-dev-shm-usage', '--no-sandbox', '--headless', '--disable-gpu'],
executablePath: pathToChrome}
);
const page = await browser.newPage();
const content = await page.setContent(pdfOptions.dom);
const addCss7 = await page.addStyleTag({path: appPath + '/public/css/framework.css'});
const addCss8 = await page.addStyleTag({path: appPath + '/public/css/print-media.css'});
const buffer = await page.pdf();
F.log(buffer);
Run Code Online (Sandbox Code Playgroud)
在 css 文件夹中,我创建了一个包含 font-awesome 字体的 fonts 文件夹,@font-face 引用了此路径:
const browser = await puppeteer.launch({
args: ['--disable-dev-shm-usage', …Run Code Online (Sandbox Code Playgroud) 我将一些剧作家测试从 js 中的 Visual Studio 代码移植到使用 xuint 的 Visual Studio C#。
我似乎无法让 Expect 命令在 Visual Studio 中工作。
在 javascript 项目中,我将执行以下操作来检查元素是否包含一些文本:
await expect(page.locator('elementtofind')).toContainText('myText');
Run Code Online (Sandbox Code Playgroud)
在 C# 中,如果我写同样的东西,我会在expect命令上得到“名称‘标识符’在当前上下文中不存在”错误
在 JS 项目中我的导入语句是
import { test, expect } from '@playwright/test';
Run Code Online (Sandbox Code Playgroud)
在 C# 项目中我使用:
using Microsoft.Playwright;
Run Code Online (Sandbox Code Playgroud)
其中似乎不包含期望
作为临时解决方法,我在 C# 中执行以下操作以获得相同的结果
var item1 = page.Locator("elementtofind");
Assert.Equal("myText", await item1.InnerTextAsync());
Run Code Online (Sandbox Code Playgroud)
但想在我的测试中使用预期功能。
有谁知道我如何在我的项目中实现这一点?
javascript ×5
node.js ×4
playwright ×4
puppeteer ×4
c# ×2
.net ×1
.net-core ×1
fonts ×1
jestjs ×1
pdf ×1
python ×1
testing ×1
web-scraping ×1