我有一个小问题,无法找到使用 Microsoft Playwright 框架的答案。根据文档,您可以使用以下代码获取 iframe:
const frame = page.frame('frame-login');
Run Code Online (Sandbox Code Playgroud)
但是如何使用选择器来查找 iframe 并与之交互?我需要使用 CSS 选择器来查找我的 iframe,因为它没有 id。
任何帮助表示赞赏
如何关注 puppeteer 中新打开的选项卡?我单击页面上的一个元素,打开一个新选项卡,然后 puppeteer 会专注于由于某种原因而留下的页面。如何才能专注于新打开的选项卡?我使用的是谷歌浏览器
我有一个具有这种样式颜色的元素:#dc7709,我想检查该元素的文本是否具有该颜色。我如何与木偶师或剧作家一起做到这一点?
无论我做什么,我都会收到错误(X.cookies 不是函数或 X.addCookies 不是函数)。我尝试使用上下文,page.context。browserContext 等,它总是以相同的方式结束(好吧,page.context 以及 browserContext 未定义,所以错误是不同的)。
语境:
代码:
beforeEach(async function fn() {
this.timeout(20000);
browser = await chromium.launch({ headless: false });
const context = await browser.newContext();
page = await context.newPage();
await page
.goto("http://localhost:4200/#/login", {
waitUntil: "networkidle0",
})
.catch(() => {});
Run Code Online (Sandbox Code Playgroud)
});
并在测试中:
// await context.addCookies([
// { name: "csrftoken", value: cookieToken, path: "/" },
// { name: "sessionid", value: cookieSession, path: "/" },
// ]);
// await context.cookies();
Run Code Online (Sandbox Code Playgroud) 这是关于 Playwright for Python 基本功能的问题的后续内容。
如何从下拉列表中选择一个选项?
此示例远程控制一个 vuejs 网站,该网站具有“苹果”、“香蕉”、“胡萝卜”、“橙子”等水果的下拉列表
这里我想选择选项“香蕉”
from playwright import sync_playwright
import time
URL = '<my url>'
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.newPage()
page.goto(URL)
# identify this element by ID. Wait for it first
new_selector = 'id=name-fruit'
page.waitForSelector(new_selector)
handle = page.querySelector(new_selector)
# at this point I have the element and can print the content
print(handle.innerHTML())
Run Code Online (Sandbox Code Playgroud)
下拉列表 HTML 像这样
<select data-v-c2cef47a="" id="name-fruit" autocomplete="true" class="form-select__select">
<option data-v-c2cef47a="" disabled="disabled" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用剧作家测试在选项卡之间切换,但它没有控制 windows 元素。我们有类似 playwright 中的 selenium driver.switchto().window() 的方法吗?
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: false, args: ['--start-maximized'] });
const context = await browser.newContext({ viewport: null });
context.on("page", async newPage => {
console.log("***newPage***", await newPage.title())
})
const page = await context.newPage()
const navigationPromise = page.waitForNavigation()
// dummy url
await page.goto('https://www.myapp.com/')
await navigationPromise
// User login
await page.waitForSelector('#username-in')
await page.fill('#username-in', 'username')
await page.fill('#password-in', 'password')
await page.click('//button[contains(text(),"Sign In")]')
await navigationPromise
// User lands …Run Code Online (Sandbox Code Playgroud) 我有以下源代码并在 headful 模式下运行它。我可以输入电子邮件地址。但是,之后会出现消息“无法让您登录。为了保护您的利益,您无法从此设备登录。请稍后重试,或从其他设备登录。”。
我需要设置额外的标头或其他内容吗?
这是我的源代码。
const playwright = require('playwright');
const cookiePath = '/home/ubuntu/.config/chromium/Default';
browser['chromium'] = await playwright['chromium'].launchPersistentContext(cookiePath,{
headless: false,
args: [
`--disable-extensions-except=${pathToExtension}`,
`--load-extension=${pathToExtension}`,
],
});
const page = await browser['chromium'].newPage();
const login_url = "https://accounts.google.com/signin/v2/identifier?hl=ja&flowName=GlifWebSignIn&flowEntry=ServiceLogin";
await page.goto(login_url);
await page.fill('#identifierId',userinfo['id']);
await page.click("#identifierNext");
await page.fill('[name=password]',userinfo['password']);
await page.click("#passwordNext");
Run Code Online (Sandbox Code Playgroud) 我已经开始学习playwright-python,该包playwright有两个子模块async_api和sync_api。然而,我找不到关于它们各自优缺点的更深入的描述或讨论。从他们的名字来看,我假设同步 API 调用是阻塞的,而异步 API 调用在后台运行?
它们的功能是否不同,即是否存在无法sync_api完成您可以使用 来完成的事情的情况async_api(反之亦然)?
python webautomation async-await playwright playwright-python
我正在尝试获取第 18 天的元素,并检查它是否已在其类中禁用。
<div class="react-datepicker__day react-datepicker__day--tue" aria-label="day-16" role="option">16</div>
<div class="react-datepicker__day react-datepicker__day--wed react-datepicker__day--today" aria-label="day-17" role="option">17</div>
<div class="react-datepicker__day react-datepicker__day--thu react-datepicker__day--disabled" aria-label="day-18" role="option">18</div>
Run Code Online (Sandbox Code Playgroud)
这是我的代码,假设
this.xpath = 'xpath=.//*[contains(@class, "react-datepicker__day") and not (contains(@class, "outside-month")) and ./text()="18"]'
Run Code Online (Sandbox Code Playgroud)
async isDateAvailable () {
const dayElt = await this.page.$(this.xpath)
console.log(dayElt.classList.contains('disabled'))) \\this should return true
Run Code Online (Sandbox Code Playgroud)
我似乎无法让它发挥作用。错误提示 TypeError: 无法读取未定义的属性“包含”。你能帮我指出我在这里做错了什么吗?
test.beforeAll(async ({ page }) => {
// Go to the starting url before each test.
await page.goto('https://my.start.url/');
});
Run Code Online (Sandbox Code Playgroud)
playwright-test runner 中有没有一种很好的方法来设置浏览器并导航到与页面对象相关的页面以运行该文件中的测试?
如果可能的话,我想避免为每个人做这个“test.beforeEach”。目前在剧作家测试中不可能做到这一点,但在开玩笑中是可能的。有任何想法吗?
Property 'page' does not exist on type 'PlaywrightWorkerArgs & PlaywrightWorkerOptions'.
Run Code Online (Sandbox Code Playgroud) playwright ×10
javascript ×6
puppeteer ×3
node.js ×2
python ×2
async-await ×1
dom ×1
e2e-testing ×1