how do I assign specific json data for web automation? - javascript

I'm using puppeteer to do web automations. I'm automating the task to fill up a form. My script is set to open multiple browsers so as to automate filling the form alot faster. However I need each browser automation to use separate information. Here's the code below.
for(let i = 0; i<json.length; i++){
await page.type('#firstName', json[i].firstName);
await page.type('#lastName', json[i].lastName);
await page.type('#phone', json[i].phone);
await page.type('#address1', json[i].unitNumber);
await page.type('#address2', json[i].address1);
await page.type('#address3', json[i].address2); //not complusory(for sg region ONLY)
await page.type('#town', json[i].townCity);
await page.type('#county', json[i].stateProvince); //not complusory(for sg region ONLY)
await page.type('#postcodeLook', json[i].postalCode);
}
Here's my json (just a template but there would be lines of information in this format.)
firstName;lastName;phone;unitNumber;address1;address2;townCity;stateProvince;postalCode
There error Im facing is that when inputing the data into the form it actually fills in the whole list of information into one of the forms input section. What I mean is , e.g for firstName it will fill in firstName1, firstName2, firstName3 and so on depending on the number of data in the json. How would I fix this? Thank you in advance

Related

Strapi Async API Request in a Loop

I'm using a headless CMS (Strapi), which forces pagination on its GET endpoints, with up to 100 entries per page on the content types. I have ~400 entries on a Location content type that I want to show on the same page. I'm trying to read each page on load and store each Location in an array, but I'm having some issues fetching this data asynchronously. Instead of getting each page, I get the first page for each of the iterations of the loop. I'm new to async-await requests, so I'm not sure how to ensure that I am reading each page's data. Thanks in advance!
const getLocationsPage = async (page) => {
return await useFetch(`${this.config.API_BASE_URL}/api/locations?sort=slug&pagination[page]=${page}&pagination[pageSize]=50`)
}
for(let i = 1; i < this.pageCount; i++){
const pageResults = await getLocationsPage(i)
console.log(pageResults)
}

How to improve PuppeteerSharp code to set input value and click button

I have to use PuppeteerSharp to login to one web site.
The web page uses JavaScript to generate dynamic class, so each time when I tried to login, I can see different class name.
So using Puppeteer Recorder is useless, I can't use its code to login.
But I checked its HTML structure, I found it is rather simple: there is one submit form with 3 input fields, the first one is input field for User_Name,
the second input field for Password, the third is button to submit the form.
So nowadays, I can use the following code to login with a lot of keyboard typings. But my code was NOT a good one, as other keyboard actions will make it a mess.
But I can't find any good PuppeteerSharp code to do something like this:
Find all 3 input fields, then type the user name in the first one, type the password in the second one, and click on the third one.
Please advise!
_page = await _bookie_browser.NewPageAsync();
await _page.SetViewportAsync((new ViewPortOptions { Width = 1920, Height = 3938 }));
await _page.GoToAsync("https://www.webpage.com");
await _page.WaitForTimeoutAsync(2000);
await _page.FocusAsync("input");
await _page.Keyboard.TypeAsync("username");
await _page.WaitForTimeoutAsync(1000);
await _page.Keyboard.PressAsync("Tab");
await _page.WaitForTimeoutAsync(1000);
await _page.Keyboard.TypeAsync("password");
await _page.Keyboard.PressAsync("Tab");
await _page.WaitForTimeoutAsync(1000);
await _page.Keyboard.PressAsync("Enter");
await _page.WaitForTimeoutAsync(1000);

How do you enter text into a Flash TextArea from Puppeteer?

I am trying to automate login in a Flash object via Puppeteer. I have automated focusing on the Flash TextArea just by clicking, and that seems to work (the vertical blinking bar that indicates that you are typing appears). However, I have tried using page.keyboard.press, page.keyboard.up/page.keyboard.down, and page.keyboard.type, and none successfully enter text into the username or password field. Furthermore, I have set a piece of injected Javascript to console.log the key name of every keypress event on the Flash object, and it only fires when I am manually typing while focused on the Flash object. It does not log anything during my attempts to use Puppeteer keyboard inputs. My code is as follows:
const login = async (page) => {
await page.waitFor(20);
const username = process.env.SIGNIN_USERNAME;
await page.click(500,500); // Select the username field
await page.waitFor(20); // Allow the blinking bar to appear
await page.keyboard.type(username);
for(let char of username) {
await page.keyboard.press(char);
await page.waitFor(20); // So that it appears like a user is typing
}
for(let char of username){
await page.keyboard.down(char);
await page.waitFor(10);
await page.keyboard.up(char);
await page.waitFor(20);
}
await page.type("object",username); // The Flash object is the first object on the page
console.log(username) // The username is logged to the console and is defined
};
This code does not result in any text appearing in the Flash TextArea. However, the correct username is logged to the console.
Am I making a mistake, or is there some general way in Puppeteer or even just in browser Javascript to enter text into a Flash TextArea that I'm missing? Thanks.
Try using a more low-level function like keyboard.sendCharacter, which doesn't deal with all the weird event handling of keyboard.press.

how to Ctrl+A page and Input that into a string

I am making a puppeteer program were it checks Google Docs
I want to be able to select all the text in the Google Docs and be able to make it into a string or a variable
I tried to Copy the text in the Google docs But didn't know where to go from there
await page2.keyboard.down('Control');
await page2.keyboard.press('KeyA');
await page2.keyboard.press('KeyC');
await page2.keyboard.up('Control');
I don't know where to go from here
try
await page.keyboard.down('ControlLeft')
await page.keyboard.press('KeyA')
await page.keyboard.press('KeyC');
await page.keyboard.up('ControlLeft')

Click on an dropdown menu

Hello,
I´m currently working on an Javascript based on Nodejs where I use Puppeteer as an help to scrape the web. As you can read in the title I´m trying to click on a dropdown menu Item, where the dropdown changes if you type in something diffrent. First here is my code:
// Navigate to the Homepage
await page.goto('https://www.futbin.com/');
await page.click('#player_search');
await page.keyboard.type(playerName);
await page.keyboard.press('ArrowDown');
await page.keyboard.press('ArrowDown');
// Create a screenshot
await page.screenshot({
path: 'screenshot.png'
});
So Basically i do the screenshot just for proving that the headless-browser does the right thing.
The website is futbin, if you want to see how their website works and take a look at the inspect, i think that could help.
But my real problem is that normally when you press enter you directly go to the player page (where i want to get). but after my script there comes always the error "no target". So the keyboard.press('Enter') don´t works. Also other Suggestions from SO didn´t work for me as the dropdown isn´t native and don´t hast counting indexes.
I would really appreciate some Suggestions !
at the end i wanted leave the html code from the first row of the dropdown as well, but i never worked so i would appreciate if you´d take a look at the website please !
You were very close, you dont need to use the arrows as you can find the item to click another way. The secret is to add a waitForSelector as the dropdown makes a call to a api endpoint. Also notice the waitForSelector on the final page to render before we take the screenshot.
Therefore just do this:-
const puppeteer = require('puppeteer');
async function run() {
const browser = await puppeteer.launch( {
headless: false
});
const page = await browser.newPage();
await page.goto('https://www.futbin.com/');
await page.type('#player_search', "Dave");
await page.waitForSelector("ul li a[data-id]");
await page.click("ul li a[data-id]");
await page.waitForSelector('#cal');
await page.screenshot( { path: "./dave.png"});
await browser.close();
};
run();
EDIT ADDTIONAL
To select any index use:-
let index = 3;
let selector = "ul li:nth-child(" + index +") a[data-id]"
await page.click(selector);
This goes to the third item in the dropdown. HTH

Categories