Puppeteer:无法 select 并使用数据属性单击按钮
Puppeteer: unable to select and click button using data attribute
我有一个按钮的代码,它将具有动态生成的 ID 和 class。
<a role="button" class="_42ft _4jy0 _6lti _4jy6 _4jy2 selected _51sy" href="#" ajaxify="/reg/spotlight/" id="u_0_2_zs" data-testid="open-registration-form-button" rel="async">Create account</a>
我正在尝试 select 并使用 puppeteer 单击它但没有成功,我已经尝试过此代码但脚本将以错误结束,因为该元素未 selected .
(async () => {
//
const browser = await puppeteer.launch({
headless: false
});
//
const page = await browser.newPage();
//
await page.goto('https://example.com/login', {
waitUntil: ['load', 'networkidle2']
});
//
const cookieButton = await page.$('[data-cookiebanner="accept_button"]');
await cookieButton.click();
//
const profileButton = await page.$('[data-testid="open-registration-form-button"]')
await profileButton.click();
})();
有select按钮点击的解决方法吗?
只要页面上有动态呈现的 css 选择器,我就会使用 xpath 路由。
第 1 步 => 通过检查元素找到 xPath,右键单击该元素 >“复制 xpath”
第 2 步 => 使用 page.waitForXPath(xpath[ options]) 和 page.$x() 函数,可在 puppeteer 文档 https://devdocs.io/puppeteer/index#pagexexpression
中找到
第 3 步 => Run/Test 您的代码
我有一个按钮的代码,它将具有动态生成的 ID 和 class。
<a role="button" class="_42ft _4jy0 _6lti _4jy6 _4jy2 selected _51sy" href="#" ajaxify="/reg/spotlight/" id="u_0_2_zs" data-testid="open-registration-form-button" rel="async">Create account</a>
我正在尝试 select 并使用 puppeteer 单击它但没有成功,我已经尝试过此代码但脚本将以错误结束,因为该元素未 selected .
(async () => {
//
const browser = await puppeteer.launch({
headless: false
});
//
const page = await browser.newPage();
//
await page.goto('https://example.com/login', {
waitUntil: ['load', 'networkidle2']
});
//
const cookieButton = await page.$('[data-cookiebanner="accept_button"]');
await cookieButton.click();
//
const profileButton = await page.$('[data-testid="open-registration-form-button"]')
await profileButton.click();
})();
有select按钮点击的解决方法吗?
只要页面上有动态呈现的 css 选择器,我就会使用 xpath 路由。
第 1 步 => 通过检查元素找到 xPath,右键单击该元素 >“复制 xpath”
第 2 步 => 使用 page.waitForXPath(xpath[ options]) 和 page.$x() 函数,可在 puppeteer 文档 https://devdocs.io/puppeteer/index#pagexexpression
中找到第 3 步 => Run/Test 您的代码