[英]How to extract the text 2 from the html using Selenium and Java?
Using Java with Selenium and Eclipse and Chrome. 在Selenium,Eclipse和Chrome中使用Java。 I was on the page in Chrome I wanted to find an xpath for, so I did an inspect, and then right-click->copy->xpath and it returned the following:
我当时在Chrome上想要查找xpath的页面上,所以进行了检查,然后右键单击-> copy-> xpath,它返回以下内容:
//*[@id="queueForm"]/div[1]/span[3]/text()[2]
So I did a 所以我做了一个
// driver is an instance of ChromeDriver
String xp = "//*[@id="queueForm"]/div[1]/span[3]/text()[2]";
WebElement ele = driver.findElement(By.xpath(xp));
and I received an error message that that returned an object and not WebElement. 并且我收到一条错误消息,该错误消息返回了一个对象而不是WebElement。 Anyone run into this situation before?
有人遇到过这种情况吗? The HTML is like
HTML就像
<span class="displayOption" style="float:right;">
Page <input id="gotoresultpage" type="text" onkeypress="javascript:fncCaptureEnter(this);" size="1" maxlength="4" tabindex="1" value="1" class="navigationMenu" style="width:20px;text-align:right"> of 2
<input type="button" tabindex="1" value="Go" id="pageGo" name="pageGo" class="navigationButton" onclick="g_objQueueUiController.fncGo();">
<span class="recordNavigationBack"></span>
<span class="recordNavigationForwardEnabled" id="pageNext" name="pageNext" onclick="g_objQueueUiController.fncGoToPage('2')"></span>
</span>
You can see text "Page" in there and a bit later "of 2". 您可以在其中看到文本“ Page”,稍后可以看到“ of 2”。 The page number is in the input box.
页码在输入框中。
So I am trying to get the "of 2" text. 因此,我尝试获取“ 2个”文本。 I already got the page number from the value of the input box, but not sure how to get the "of 2".
我已经从输入框的值中获得了页码,但是不确定如何获取“ 2”。
try this 尝试这个
xpath = //span[@class='displayOption']//input[@id='gotoresultpage']
driver.findElement(By.xpath(xpath))).getAttribute("value");
Remove this text()
from the end of your XPath expression, the value returned from the WebDriver.find() function must be a Node , not the text. 从XPath表达式的末尾删除此
text()
,从WebDriver.find()函数返回的值必须是Node ,而不是文本。
So you need to amend your code to something like: 因此,您需要将代码修改为:
//input[@id='gotoresultpage']
example code: 示例代码:
WebElement someElement = driver.findElement(By.xpath("//input[@id='gotoresultpage']"))
String text = someElement.getText();
More information: 更多信息:
While running Selenium Web-driver, sometimes the page takes a bit more time to load DOM at the back-end and page itself which causes the web-driver to fail a test step. 在运行Selenium Web驱动程序时,有时页面需要花费更多时间在后端和页面本身上加载DOM,这导致Web驱动程序无法通过测试步骤。 Try using 'Waits' in your code so that it provides a bit more time to load your page and DOM.
尝试在代码中使用“等待”,以便它提供更多时间来加载页面和DOM。 Try using this code for wait:
尝试使用以下代码等待:
WebDriverWait wait = new WebDriverWait(driver, 15); WebDriverWait wait =新的WebDriverWait(driver,15); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(“”))));
Or simply use thread.sleep: Thread.sleep(Long Millis, Int nanos); 或者简单地使用thread.sleep:Thread.sleep(Long Millis,Int nanos);
(Example: Thread.sleep(5000, 50000);this will implement 5 seconds sleep between your test steps) (示例:Thread.sleep(5000,50000);这将在您的测试步骤之间实现5秒钟的睡眠)
Also, pls note that auto generated xpaths are dynamic which may update on your next test script execution so be vigilant while selecting a web element to be found through web-driver. 另外,请注意,自动生成的xpath是动态的,可能在您下一次测试脚本执行时更新,因此在选择要通过Web驱动程序找到的Web元素时要保持警惕。
The text of 2 is a text node , so to extract the text you need to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following Locator Strategies : 2的文本是一个文本节点 ,因此要提取文本,需要为visible_of_element_located
visibility_of_element_located()
引入WebDriverWait ,并且可以使用以下任一定位器策略 :
Using cssSelector
: 使用
cssSelector
:
System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[3].textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#queueForm>div span.displayOption[style*='right']")))).toString());
Using xpath
: 使用
xpath
:
System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[3].textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id="queueForm"]/div//span[@class='displayOption' and contains(@style, 'right')]")))).toString());
I did the test in following link : https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_form 我在以下链接中进行了测试: https : //www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_form
Here is the HTML 这是HTML
<!DOCTYPE html> <html> <body> <form action="/action_page.php" id="form1"> First name: <input type="text" name="fname"><br> <input type="submit" value="Submit"> </form> <p>The "Last name" field below is outside the form element, but still part of the form.</p> Last name: <input type="text" name="lname" form="form1"> </body> </html>
I got the text "First name" of form 我得到了表格的“名字”文字
Here my code 这是我的代码
driver.switchTo().frame("iframeResult");
driver.findElement(By.id("form1")).getText();
Last line return "First name" 最后一行返回“名字”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.