[英]How to extract the dynamic text 3204255 from the html provided through Selenium?
我正在尝试从div中获取数字。执行测试用例时,将显示新的数字,现在我要获取该数字,或者换句话说,将其打印在控制台中。
我正在使用:
webElement webelement=driver.findElement(By.xpath("//div[contains(@class,'responsive now') and text()='Application number']"));
webelement.getText();
和
driver.findElement(By.xpath("//div[@class='responsive-show']")).getAttribute("value");
它与定位器不兼容,但未显示编号。
<td>
<div class="responsive-show">Application number</div>
"3204255"
<input data val="true" data-val-number="The field ExaminationNumber must be a number." data-val-required="The ExaminationNumber field is required." id="ActiveExamModeIs_0__ExaminationNumber" name="ActiveExamMode[0].ExaminationNumber" type="hidden" value="3204255">
<input data -val="true" data-val-number="The field ExaminationEligibilityExamTypeId must be a number" data-val-required="The ExaminationEligibilityExamTypeId fiels is required." type="hidden" value="1">
</td>
按照共享的HTML提取数字,即3204255,您需要诱使WebDriverWait使所需的元素可见,然后可以按照以下解决方案使用executeScript()
方法:
Java解决方案:
WebElement myElement = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@class='table table-hover']//tbody/tr/td"))); String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[2].textContent;', myElement).toString();
总而言之:除非您想使用“ JavascriptExecutor”,否则没有简单的方法可以使用简单的Selenium函数获取文本值。
另一种选择是越来越从整个文本<td>
使用简单的硒操作&然后使用字符串函数元素substring()
来得到被引号之间卡住你的应用程序数量值"
。
码:
String input = driver.findElement(By.xpath("//table[@class='table table-hover']/tbody/tr/td")).getAttribute("innerText");
input = input.substring(input.indexOf("\"")+1, input.lastIndexOf("\""));
System.out.println("Extracted Value" + input);
文本:3204247不是包含在div标签中,而是包含在<td>
。 您可以尝试以下方法:
webElement webelement=driver.findElement(By.xpath("//table[@class='table table-hover']/tbody/tr/td"));// you can improve this locator and can also use one below
//webElement webelement=driver.findElement(By.xpath("//div[@class='responsive-show']/..")); using xpath axes
String value=webelement.getText();// OR
//String value=webelement.getAttribute("innerText");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.