const { test, expect } = require('@playwright/test');
test('has title', async ({ page }) => {
//Opens zillow and log the user in
await page.goto('https://www.zillow.com');
// await page.pause();
// //It closes the google popup
await page.frameLocator('iframe[title="Sign in with Google Dialog"]').getByLabel('Close').click();
await page.getByRole('link', { name: 'Sign In' }).click();
//Add your email and password to the fill strings
await page.getByPlaceholder('Enter email').fill("NEED EMAIL");
await page.getByPlaceholder('Enter password').fill("NEED PASS");
await page.getByRole('button', {name: 'Sign in'}).click();
await page.waitForTimeout(2000);
//Searchbar fill
await page.getByRole('button', { name: 'Got it!' }).click();
await page.getByPlaceholder('Enter an address, neighborhood, city, or ZIP code').fill("Chattanooga");
await page.waitForTimeout(2000);
await page.getByRole('button', {name: 'Submit Search'}).click();
await page.waitForTimeout(2000);
// await page.pause();
const ulElement = await page.$$('.List-c11n-8-84-3__sc-1smrmqp-0.StyledSearchListWrapper-srp__sc-1ieen0c-0.doa-doM.fgiidE.photo-cards.photo-cards_extra-attribution li');
if (ulElement.length > 0) {
// Process each li element in the array
for (const liElement of ulElement) {
const textContent = await page.evaluate(li => li.textContent, liElement);
if (textContent?.includes('37421')) {
const button = await liElement.$('button');
if (button) {
await button.click();
console.log('Text content of li element:', textContent);
}
}
}
} else {
console.log('No li elements found inside the ul with role="list".');
}
});
我尝试添加诸如等待页面之类的超时。但是它不起作用
ful更多指针:注:您可以根据情况调整超时参数值。 使用示例
// A specific element is visible. await expect(page.getByText('Welcome'+ userName)).toBeVisible({timeout:5000}) // At least one item in the list is visible. await expect(page.getByTestId('todo-item').first()).toBeVisible() // At least one of the two elements is visible, possibly both. await expect( page.getByRole('button', { name: 'Sign in' }) .or(page.getByRole('button', { name: 'Sign up' })) .first() ).toBeVisible()
不使用硬编码等待,而是使用loscators sovertions
,因为它们是动态的,不会不必要地等待。作为动态,因此您不会陷入“陈旧元素参考错误”之类的问题(如果您来自Selenium World,您会理解它!) 不使用“长度”之类的JS级别属性,而是使用playwright API方法(例如count