I'm trying to retrieve text of an element with == $0. I tried to use JS injection
((JavascriptExecutor) driver).executeScript("return arguments[0].value", driver.findElement(By.cssSelector("div.settings span")))
but this approach returns NULL for me
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
As the texts Shooting and Pedestrian Accident are within elements which are text nodes, so to extract the texts you can use the following solutions:
WebElement myElement = driver.findElement(By.cssSelector("div.settings"));
//extracting Shooting
System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].firstChild.textContent;", myElement).toString());
//extracting Pedestrian Accident
System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", myElement).toString());