Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { webkit } = require('playwright');
- const BASE_URL = 'https://twitter.com/';
- const USER_NAME = 'Reverse1999_JP';
- const SCREENSHOT_PATH = '/Users/anon/Downloads/';
- (async () => {
- let browser = null; // Define the browser variable outside the try block
- try {
- browser = await webkit.launch({
- headless: true // Set headless mode
- });
- const context = await browser.newContext({
- viewport: { width: 1280, height: 10000 } // Set window size
- });
- const page = await context.newPage();
- // Navigate to URL
- await page.goto(`${BASE_URL}${USER_NAME}`);
- // Wait for the div element with attribute data-testid="cellInnerDiv" to appear
- await page.waitForSelector('[data-testid="cellInnerDiv"]');
- await page.waitForTimeout(1000); // Wait for 1 second
- // Retrieve URLs from div elements and store them in an array
- const postIDs = await page.$$eval('[data-testid="cellInnerDiv"]', (divs, { userName }) => {
- return divs.map(div => {
- const link = div.querySelector(`a[href*="/${userName}/status/"]`);
- return link ? link.getAttribute('href').replace(`/${userName}/status/`, '') : null;
- }).filter(href => href !== null);
- }, { userName: USER_NAME }); // Pass USER_NAME as an argument to the callback function
- for (const postID of postIDs) {
- await page.goto(`${BASE_URL}${USER_NAME}/status/${postID}`);
- await page.waitForSelector('[data-testid="cellInnerDiv"]');
- // Retrieve the text of the div element
- const postContent = await page.$('[data-testid="cellInnerDiv"]');
- const postText = await postContent.textContent();
- // Check if the text contains "コード"
- if (postText.includes('コード')) {
- // Save a screenshot of the element
- await postContent.screenshot({ path: `${SCREENSHOT_PATH}${USER_NAME} ${postID}.png` });
- }
- }
- } catch (error) {
- console.error('Error occurred:', error);
- } finally {
- // Close the browser if it's defined
- if (browser) {
- await browser.close();
- }
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement