1

Element.text returns the text content of an element.

In a different thread on SO, the python script scrapes data from the followers modal on an Instagram account. The following part returns the text inside the lists and stores them in an array.

 xpath = "//div[@style='position: relative; z-index: 1;']//ul/li/div/div/div/div/a"
 followers_elems = driver.find_elements_by_xpath(xpath)
 return [e.text for e in followers_elems]

I'm trying to achieve a similar result in JavaScript (I'm using WebDriverJS) :

 const XPATH = "/html/body/div[3]/div/div[2]/div/div[2]/ul/div/li";
 var followers_elems = await driver.findElements(By.xpath(XPATH));
 var followers_temp = [];
 for (var e in followers_elems) {
  followers_temp.push(e.textContent); }
 console.log(followers_temp);

I'm not sure if textContent is the right property for .text .

I've tried a million different alternatives but all I'm getting is undefined values in the array :

enter image description here

I'm not very proficient with JS yet, but I'm sure e is reading from followers_elems and if I push just e inside the array it can log the total follower numbers just fine. It is getting the text value from xpath that I'm not understanding. Python does this so elegantly but despite the verbose JavaScript is failing me.

Thank you.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Barani
  • 83
  • 1
  • 8
  • Try logging `e` and see which property name contains the text you want. In JS, you can do: `console.log(e);` – code Jun 29 '18 at 17:26
  • Thanks Abhishek. The text I want is the textContent from xpath. The usernames of those who follow me. In fact my above code intends to get all the readable texts from each
  • in the followers modal. Logging `e` would give me the followers count. But I need the text with usernames inside the `followers_temp` array.
  • – Barani Jun 29 '18 at 17:57
  • Are you running await inside of an async function? – mccambridge Jun 29 '18 at 18:01
  • Yes. I've got the rest of the part running smooth in JS. Would posting the entire script somewhere be more helpful? – Barani Jun 29 '18 at 18:12
  • I have posted a function below, if you grab the for-loop part of it it will work. – PJAutomator Jun 29 '18 at 18:15