Advertisement
DX3FD0XVN739

Playwright

Apr 2nd, 2024 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.07 KB | Source Code | 0 0
  1. const { webkit } = require('playwright');
  2.  
  3. const BASE_URL = 'https://twitter.com/';
  4. const USER_NAME = 'Reverse1999_JP';
  5. const SCREENSHOT_PATH = '/Users/anon/Downloads/';
  6.  
  7. (async () => {
  8.   let browser = null; // Define the browser variable outside the try block
  9.   try {
  10.     browser = await webkit.launch({
  11.       headless: true // Set headless mode
  12.     });
  13.     const context = await browser.newContext({
  14.       viewport: { width: 1280, height: 10000 } // Set window size
  15.     });
  16.     const page = await context.newPage();
  17.  
  18.     // Navigate to URL
  19.     await page.goto(`${BASE_URL}${USER_NAME}`);
  20.  
  21.     // Wait for the div element with attribute data-testid="cellInnerDiv" to appear
  22.     await page.waitForSelector('[data-testid="cellInnerDiv"]');
  23.     await page.waitForTimeout(1000); // Wait for 1 second
  24.  
  25.     // Retrieve URLs from div elements and store them in an array
  26.     const postIDs = await page.$$eval('[data-testid="cellInnerDiv"]', (divs, { userName }) => {
  27.       return divs.map(div => {
  28.         const link = div.querySelector(`a[href*="/${userName}/status/"]`);
  29.         return link ? link.getAttribute('href').replace(`/${userName}/status/`, '') : null;
  30.       }).filter(href => href !== null);
  31.     }, { userName: USER_NAME }); // Pass USER_NAME as an argument to the callback function
  32.  
  33.     for (const postID of postIDs) {
  34.       await page.goto(`${BASE_URL}${USER_NAME}/status/${postID}`);
  35.       await page.waitForSelector('[data-testid="cellInnerDiv"]');
  36.       // Retrieve the text of the div element
  37.       const postContent = await page.$('[data-testid="cellInnerDiv"]');
  38.       const postText = await postContent.textContent();
  39.  
  40.       // Check if the text contains "コード"
  41.       if (postText.includes('コード')) {
  42.         // Save a screenshot of the element
  43.         await postContent.screenshot({ path: `${SCREENSHOT_PATH}${USER_NAME} ${postID}.png` });
  44.       }
  45.     }
  46.   } catch (error) {
  47.     console.error('Error occurred:', error);
  48.   } finally {
  49.     // Close the browser if it's defined
  50.     if (browser) {
  51.       await browser.close();
  52.     }
  53.   }
  54. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement