[英]Javascript to get hidden text value from website using Selenium web driver
How can i get this telefoon nummer in string variable. 我如何在字符串变量中获取此Telefoon nummer。
<span itemprop="telephone" class="hidden" id="telefoon_28836_hidden">010 - 79 53 364</span>
I tried this code without ant success. 我尝试了这段代码,但没有成功。
JavascriptExecutor jse = (JavascriptExecutor)driver;
String script = "return document.getElementByXPath('//span[@itemprop='telephone']').getText();";
String telno1 = ((JavascriptExecutor) driver).executeScript(script).toString();
I am getting this exception for String telno1 line 我正在为String telno1行获取此异常
ErrorHandler$UnknownServerException: missing ) after argument list
please any help thank you 请任何帮助谢谢
You can use this, 你可以用这个
String theTextIWant = ((JavascriptExecutor) driver).executeScript("return arguments[0].value;",driver.findElement(By.xpath("//span[@itemprop='telephone']")));
You just use javascript to extract out the value attribute of the input field. 您只需要使用javascript提取输入字段的value属性即可。 If it were a div or textarea, then you'd like use the innerHTML, innerText, or textContent attributes.
如果它是div或textarea,那么您想使用innerHTML,innerText或textContent属性。 like below,
像下面一样
String theTextIWant = ((JavascriptExecutor) driver).executeScript("return arguments[0].innerHTML;",driver.findElement(By.xpath("//span[@itemprop='telephone']")));
You are mixing a number of things here. 您在这里混合了很多东西。 You cannot just copy/paste Selenium code and expect it to work in JavaScript.
您不能只复制/粘贴Selenium代码并期望它可以在JavaScript中工作。
My suggestion would be to be to first find the element using Selenium's XPath mechanism, and then pass that into a JavaScript string. 我的建议是首先使用Selenium的XPath机制查找元素, 然后将其传递到JavaScript字符串中。 You are merely working against Selenium's API because it allows you to find elements in variety of ways and it will transform that into an object that can be used in JavaScript for you if you want to.
您对硒的API只是工作,因为它可以让你找到各种方法元素,它会变换为可在JavaScript中,如果你想使用你的对象。
JavascriptExecutor jse = (JavascriptExecutor)driver;
WebElement element = driver.findElement(By.xpath("//span[@itemprop='telephone']"));
jse.executeScript("return arguments[0].text", element);
(Untested, unverified code, I do not have a Java IDE to handle, may not compile) (未经测试,未经验证的代码,我没有Java IDE可以处理,可能无法编译)
The inner xpath string does not seems to be formed properly. 内部xpath字符串似乎格式不正确。 This is because of the 'Single Quotes' used:
这是因为使用了“单引号”:
Try replacing: 尝试更换:
String script = "return document.getElementByXPath('//span[@itemprop='telephone']').getText();";
With: 带有:
String script = "return document.getElementByXPath(\"//span[@itemprop='telephone']\").getText();";
^ ^
Try this 尝试这个
driver.findElement(By.id("telefoon_28836_hidden")).getText();
If works, let us know. 如果可行,请告诉我们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.