简体   繁体   English

使用Selenium Web驱动程序从网站获取隐藏文本值的Javascript

[英]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. 如果可行,请告诉我们。

This will cause error: document.getElementByXPath is not a function 这将导致错误: document.getElementByXPath is not a function

Use document.evaluate insteads. 改用document.evaluate Answer of yckart in this link is helpful. 在此链接中对yckart的回答很有帮助。

Hope this helps! 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在硒Web驱动程序中使用Javascript设置选择值 - Unable to set select value Using Javascript in selenium web driver 如何使用 Python 驱动程序在 Selenium 和 web 驱动程序中获取部分文本 - How to get a part of text in Selenium and web driver using Python 如何使用 selenium 和 python 从网站获取工具提示文本,其中文本来自 javascript - How can I get the tooltip text from a website using selenium and python where the text comes from a javascript Python Selenium 使用 javascript 网站获取工具提示文本的值 - Python Selenium get value of tooltip text with javascript website 在 javascript 中使用 Selenium 驱动程序获取 WebElement 文本内容 - Get WebElement text content with Selenium Driver in javascript 无法通过使用 javascript 执行程序和 web 驱动程序从某些段落中获取可见文本 - Not able to get visible text from some paragraph by using javascript executor and web driver 如何从量角器Web驱动程序获取文本字段值 - how to get text field value from protractor web driver 在Python中使用Selenium访问JavaScript网站中的隐藏表 - Using Selenium in Python to access hidden table in JavaScript website 如何防止使用Selenium Web驱动程序或Javascript重新加载页面? - How to prevent page from reload using selenium web driver or using Javascript? 如何使用javascript从gridview获取隐藏字段的值? - how to get the value of a hidden field from a gridview using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM