What is the JavaScript equivalent of python's .text?
What is the JavaScript equivalent of python's .text?
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 :
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.
e
followers_elems
e
Thank you.
e
console.log(e);
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 <li> 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 at 17:57
e
followers_temp
Are you running await inside of an async function?
– mccambridge
Jun 29 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 at 18:12
I have posted a function below, if you grab the for-loop part of it it will work.
– PJAutomator
Jun 29 at 18:15
1 Answer
1
WebElement.getText() is the javascript equivalent of getting text element in python when using WebdriverJS.
This should work for you. I have created an async function and easy to understand for loop. Also, since getText() return a promise so i am using async - await to get the text and push it to the followers_temp array.
async function pushTextIn() {
for (let i = 0; i < followers_elems.length; i++) {
let text = await followers_elems[i].getText();
console.log('pushing text: ', text);
followers_temp.push(text);
}
return followers_temp;
}
Awesome. This works !! :) Thanks mate.
– Barani
Jun 29 at 18:36
Pleasure to help, can you upvote the answer if it helped thanks.
– PJAutomator
Jun 29 at 18:36
Sure thing! I tried getText() method earlier and resolved the promise with .then(). But I was getting typeerrors that getText is not a function.
– Barani
Jun 29 at 18:39
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try logging
e
and see which property name contains the text you want. In JS, you can do:console.log(e);
– Abhishek Soni
Jun 29 at 17:26