为了账号安全,请及时绑定邮箱和手机立即绑定

如何从Selenium提供的html中提取动态文本3204255?

如何从Selenium提供的html中提取动态文本3204255?

慕标琳琳 2021-08-19 18:46:51
我正在尝试从 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");它不是关于定位器的compailining,但它没有显示数字。<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>
查看完整描述

3 回答

?
慕的地8271018

TA贡献1796条经验 获得超4个赞

长短不一:除非您想使用“JavascriptExecutor”,否则没有一种使用简单的 Selenium 函数获取文本值的简洁方法


另一种选择是<td>使用简单的硒操作从元素中获取整个文本,然后使用 String 函数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);


查看完整回答
反对 回复 2021-08-19
?
慕田峪7331174

TA贡献1828条经验 获得超13个赞

根据您共享的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();



查看完整回答
反对 回复 2021-08-19
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

text: 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");


查看完整回答
反对 回复 2021-08-19
  • 3 回答
  • 0 关注
  • 220 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信