0

I would like to open URLs based on IDs available in Array and Obtain each unique PageSource.

On Executing the below mentioned Code, 5 URL's were opened sequentially but I'm getting Pagesource of only last element

Eg) If body of each url be like

{ID: 1, name: "ABC"}

{ID: 2, name: "CDE"}

{ID: 3, name: "EFG"}

{ID: 4, name: "HIJ"}

{ID: 5, name: "KLM"}

Result:

{ID: 5, name: "KLM"}

{ID: 5, name: "KLM"}

{ID: 5, name: "KLM"}

{ID: 5, name: "KLM"}

{ID: 5, name: "KLM"}

//EmployeeIDs
const EmployeeIDS = ["1","2","3","4","5"]
//URL
const URL = "http://example.com/data?source=employee_id&employee_id="

function EmployeeDetails(URL, EmployeeIDs){
    EmployeeIDs.map((ID) => {
        const EmployeeURL = `${URL}ID`;
        driver.get(EmployeeURL)
            .then(() => driver.findElement(By.css("body")).getAttribute("outerHTML")
                .then((result) => console.log(result))
                .catch((error) => console.log(error)))
            .catch((error) => console.log(error))
})

1 Answers1

0

When i remove your driver part and just execute the map, it doesn't work as you may be expecting. This might be why you're not getting the right data.

This code:

const EmployeeIDS = ["1","2","3","4","5"];
const URL = "http://example.com/data?source=employee_id&employee_id=";

EmployeeIDS.map((ID) => {
    const EmployeeURL = `${URL}ID`;
    console.log(EmployeeURL)
});

Returns:

[Running] node "c:\Git\JavascriptSelenium\stackoverflow_map.js"
http://example.com/data?source=employee_id&employee_id=ID
http://example.com/data?source=employee_id&employee_id=ID
http://example.com/data?source=employee_id&employee_id=ID
http://example.com/data?source=employee_id&employee_id=ID
http://example.com/data?source=employee_id&employee_id=ID

Note that it's not rendering ID. It's navigating the same URL each time - which is why you're getting the same output.

I note that I can't navigate to the url you've provided, so what happens when you go to that page? What data does it return?

A quick modification on this line:

const EmployeeURL = `${URL}`+ID;

returns this:

[Running] node "c:\Git\JavascriptSelenium\stackoverflow_map.js"
http://example.com/data?source=employee_id&employee_id=1
http://example.com/data?source=employee_id&employee_id=2
http://example.com/data?source=employee_id&employee_id=3
http://example.com/data?source=employee_id&employee_id=4
http://example.com/data?source=employee_id&employee_id=5

Give it a go and let us know. Try and make sure your map works before navigating.

Also, a really minor point is map isn't designed to be iterated in loop - you're meant to return it to a new array and use that.

RichEdwards
  • 3,423
  • 2
  • 6
  • 22
  • Hi sir!.. Thanks for your reply!.. On adding question, i made a mistake. const EmployeeURL = `${URL}ID`; it should be const EmployeeURL = `${URL}${ID}`; Once again, thanks for your time to go through my question and for your suggestions!.. I solved above mentioned problem, by using *Async/Await* instead of *Promises* sir!.. – Mohammad Jul 26 '20 at 18:37