I’m tasked with scraping a website where I need to log in and then perform an API call while within the page context. However, my fetch request doesn’t seem to finish and remains in a pending state.
Here’s my approach:
- Navigate to the login URL
- Input login details and submit the form
- Wait for the page to redirect to the dashboard
- Perform a POST request using fetch within the page context
Here’s my current implementation:
await browser.goto('https://example-login.com/signin')
await Promise.all([
browser.evaluate(() => {
(function() {
const setInputValue = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
"value").set;
const fillField = (field, value) => {
const fieldElement = document.querySelector(`[name="${field}"]`)
setInputValue.call(fieldElement, value)
fieldElement.dispatchEvent(new Event('change', { bubbles: true}))
};
fillField('email', "[email protected]");
fillField('pass', "mypassword123");
document.getElementById("login-btn").click();
})()
}),
browser.waitForNavigation({waitUntil: 'domcontentloaded'}),
]);
await browser.evaluate(() => {
fetch('https://api.example.com/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(this.requestPayload)
}).then(response => {
return response.json();
}).then(data => {
console.log(data);
})
})
The issue: The fetch call does not resolve, causing the script to hang indefinitely. It seems related to how promises function within page.evaluate(). Any suggestions on resolving this?