最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get text of an HTML-Element with SeleniumNode.js - Stack Overflow

programmeradmin0浏览0评论

Unfortunately I did not find any existing solution for my problem on stackoverflow. So, can someone help.

I want to get the text of an html-element. I tried this:

var element = driver.findElement(webdriver.By.className("credits")).getText();

console.log(element);

Unfortunately this did not work so far.

Unfortunately I did not find any existing solution for my problem on stackoverflow. So, can someone help.

I want to get the text of an html-element. I tried this:

var element = driver.findElement(webdriver.By.className("credits")).getText();

console.log(element);

Unfortunately this did not work so far.

Share Improve this question edited Aug 16, 2017 at 5:54 NarendraR 7,70811 gold badges47 silver badges88 bronze badges asked Aug 15, 2017 at 17:37 TonisalTonisal 1911 gold badge1 silver badge6 bronze badges 3
  • 1 Can yo post the html that you are trying to get the text from? – smit9234 Commented Aug 15, 2017 at 17:59
  • Set var element = driver.findElement(webdriver.By.className("credits")).innerHTML – Luke Danger Kozorosky Commented Aug 15, 2017 at 17:59
  • just use await if you are an async function. – Makan dianka Commented Jun 20, 2024 at 10:49
Add a comment  | 

3 Answers 3

Reset to default 14

Someone posted an answer and deleted it seconds after it...thank you anyway:

For you information, this works fine for me:

var textPromise = driver.findElement(webdriver.By.className("credits")).getText();
textPromise.then((text) => {
  console.log(text);
});

Try this instead if you are inside an async function:

var element = await driver.findElement(webdriver.By.className("credits")).getText();

console.log(element);

You have to be inside of async function and use await

let elementName = await this.driver.findElement(By.css(`.factoryListElementName`)).getText()

console.log(elementName)
发布评论

评论列表(0)

  1. 暂无评论