简体   繁体   中英

getText() of WebElement in Selenium (JS) fails

I've been encountering an issue with trying to extract plain text from a WebElement using JavaScript selenium-webdriver. I'm currently using MicrosoftEdge browser and driver, everything is set up properly but I can't work out why trying to get values from WebElement is broken in JavaScript Selenium. For example, suppose the following document:

<html>
    <body>
        <p>Hello</p>
        <a href="http://bing.com/maps" class="myClass">Maps</a>
    </body>
</html>

and the subsequent querying (setup ommited for brevity):

browser.get("file:///path/to/index.html");
browser.sleep(1000); //Just to be sure...
var ep = browser.findElement(By.linkText("Maps"));
ep.then(elm => console.log(elm.getText()));   //Doesn't work!
ep.then(elm => console.log(elm.getAttribute("innerHtml")));   //Doesn't work!
ep.then(elm => elm.click());  //works

Anything that doesn't directly interact with the element (eg sendKeys or click), including calls like isDisplayed() seem to fail and give some exception trace in the console. I'm using selenium-webdrivier 3.3.0 on Windows 10 with nodejs version 6.10.0.

API for convinience.

getText and getAttribute return promises that resolve with the value requested.

elm.getText().then(function (text) {
   console.log(text);
});

Working in my aproache:

await teste.driver.findElement({
    css: 'input[name="nameElementExample"]'
  }).getAttribute("value");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM