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

我正在尝试从 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>


慕标琳琳
浏览 250回答 3
3回答

慕的地8271018

长短不一:除非您想使用“JavascriptExecutor”,否则没有一种使用简单的 Selenium 函数获取文本值的简洁方法另一种选择是<td>使用简单的硒操作从元素中获取整个文本,然后使用 String 函数substring()获取您的应用程序编号值,该值卡在引号之间"。代码:&nbsp; &nbsp; String input = driver.findElement(By.xpath("//table[@class='table table-hover']/tbody/tr/td")).getAttribute("innerText");&nbsp; &nbsp; input = input.substring(input.indexOf("\"")+1, input.lastIndexOf("\""));&nbsp; &nbsp; System.out.println("Extracted Value" + input);

慕田峪7331174

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

芜湖不芜

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 axesString value=webelement.getText();// OR//String value=webelement.getAttribute("innerText");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java