Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
This is as much as I was able to shorten it (it downloads all the visible links with \nTitle in their text): import { chromium } from 'playwright'; (async () => { const browser = await chro...
#2: Post edited
This is as much as I was able to shorten it (in downloads all the visible links with `\nTitle` in their text):- ```ts
- import { chromium } from 'playwright';
- (async () => {
- const browser = await chromium.launch();
- const context = await browser.newContext({ storageState: 'loggedInState.json'});
- const page = await context.newPage();
- await page.goto(mylink);
- const visibleLinksLocator = page.locator('a:visible');
- const linkTexts: string[] = await visibleLinksLocator.allInnerTexts();
- const filteredTexts = linkTexts.filter(linkText => linkText.includes("\nFile"));
- for (const text of filteredTexts) {
- const downloadPromise = page.waitForEvent('download');
- await page.getByText(text).click();
- const download = await downloadPromise;
- const fileName = download.suggestedFilename();
- await download.saveAs(`./downloads/${fileName}`);
- }
- await browser.close();
- })();
- ```
- This is as much as I was able to shorten it (it downloads all the visible links with `\nTitle` in their text):
- ```ts
- import { chromium } from 'playwright';
- (async () => {
- const browser = await chromium.launch();
- const context = await browser.newContext({ storageState: 'loggedInState.json'});
- const page = await context.newPage();
- await page.goto(mylink);
- const visibleLinksLocator = page.locator('a:visible');
- const linkTexts: string[] = await visibleLinksLocator.allInnerTexts();
- const filteredTexts = linkTexts.filter(linkText => linkText.includes("\nFile"));
- for (const text of filteredTexts) {
- const downloadPromise = page.waitForEvent('download');
- await page.getByText(text).click();
- const download = await downloadPromise;
- const fileName = download.suggestedFilename();
- await download.saveAs(`./downloads/${fileName}`);
- }
- await browser.close();
- })();
- ```
#1: Initial revision
This is as much as I was able to shorten it (in downloads all the visible links with `\nTitle` in their text):
```ts
import { chromium } from 'playwright';
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext({ storageState: 'loggedInState.json'});
const page = await context.newPage();
await page.goto(mylink);
const visibleLinksLocator = page.locator('a:visible');
const linkTexts: string[] = await visibleLinksLocator.allInnerTexts();
const filteredTexts = linkTexts.filter(linkText => linkText.includes("\nFile"));
for (const text of filteredTexts) {
const downloadPromise = page.waitForEvent('download');
await page.getByText(text).click();
const download = await downloadPromise;
const fileName = download.suggestedFilename();
await download.saveAs(`./downloads/${fileName}`);
}
await browser.close();
})();
```
