3 回答
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);
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();
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");
添加回答
举报