如何获取标记之间的值。这是 HTML ... 复选框值需要存储在List中。
List<WebElement> eles = driver.findElements(By.cssSelector("ul.rcbList > li"));
List<String> txts = new ArrayList<String>();
String script = "return arguments[0].lastChild.nodeValue;";
JavascriptExecutor js = (JavascriptExecutor) driver;
Boolean stale = true;
for (WebElement ele : eles) {
try {
WebElement webEle = ele.findElement(By.className("rcbCheckBox"));
if (webEle.getText().equals(sheetValue)) {
String text = (String) js.executeScript(script, ele);
txts.add(text.toString());
String txt = text.toString();
for (String texts : txts) {
System.out.println("Print----------> " + texts);
}
Thread.sleep(3000);
// if (text.equals(sheetValue)) {
System.out.println("inside if" + webEle.getText().equals(sheetValue));
ele.findElement(By.tagName("input")).click();
;
} // ele.click();
stale = false;
} catch (StaleElementReferenceException e) {
stale = true;
}
}
请帮我找代码。绑定语言是 JAVA。
我的示例代码: -
string[] index = YourString.Split(',');
我现在被困在这里。
答案 0 :(得分:1)
Tag中的文本是TextNode的类型,而不是ElementNode。 Selenium的find API只能从HTML DOM Tree返回ElementNode。
要访问TextNode,您必须通过driver.executeScript()
使用JavaScript,如下所示:
List<WebElement> eles = driver.findElements(By.cssSelector("ul.rcbList > li"));
List<String> txts = new ArrayList<String>();
String script = "return arguments[0].lastChild.nodeValue;";
JavascriptExecutor js = (JavascriptExecutor) driver;
int length = eles.size();
for(int i=0;i<length;i++) {
WebElement ele = eles.get(i);
String text = (String) js.executeScript(script, ele);
if(text.equals(sheetValue)) {
ele.findElement(By.cssSelector('input')).click();
txts.add(text);
// find all li again on page after click,
// seems each click on checkbox caused page DOM tree changed
eles = driver.findElements(By.cssSelector("ul.rcbList > li"));
}
}
或者您可以找到TextNode的父ElementNode,并获取父文本的全文,然后从全文中提取部分文本。
答案 1 :(得分:0)
使用getText()
,您可以获得WebElement的innerText。既然你在<input>
标签中没有任何文字,我想你想获得属性?
如果您想要WebElement的属性,可以使用getAttribute()
。
答案 2 :(得分:0)
这将返回Webelements List
driver.FindElement(By.XPath("html/body/div[1]/ul"));
答案 3 :(得分:0)
试试这个:
List<WebElement> checkboxes=driver.findElements(By.xpath("//ul[@class='rcbList']/li"));
for (WebElement checkbox: checkboxes) {
System.out.println(checkbox.getText());
答案 4 :(得分:0)
通过问题中的解释和查看其他答案,我认为您需要HTML标记之间的HTML内容,即 -
要获取上述HTML内容,请尝试使用以下Java代码。
final String regex = "(>\\w+((\\s\\W\\s)|(\\W)|(\\s))\\w+<)|(>\\w+<)";
final String string = "<div class=\"rcbScroll rcbWidth\" style=\"height:180px;width:100%;\">" +
"<div class=\"rcbCheckAllItems\"><input type=\"checkbox\" class=\"rcbCheckAllItemsCheckBox\">Check All</div>" +
"<ul class=\"rcbList\" style=\"list-style:none;margin:0;padding:0;zoom:1;\">" +
"<li class=\"rcbHovered \"><input type=\"checkbox\" class=\"rcbCheckBox\">Dallas , Wanda</li>" +
"<li class=\"rcbItem \"><input type=\"checkbox\" class=\"rcbCheckBox\">Parker , Tina</li>" +
"<li class=\"rcbItem \"><input type=\"checkbox\" class=\"rcbCheckBox\">Plano , Samantha </li>" +
"<li class=\"rcbItem \"><input type=\"checkbox\" class=\"rcbCheckBox\">Richardson , Christina</li>" +
"<li class=\"rcbItem \"><input type=\"checkbox\" class=\"rcbCheckBox\">Dave , Test</li>" +
"<li class=\"rcbItem \"><input type=\"checkbox\" class=\"rcbCheckBox\">clinician , test</li>" +
"<li class=\"rcbItem \"><input type=\"checkbox\" class=\"rcbCheckBox\">Smith , Jasmin</li>" +
"<li class=\"rcbItem \"><input type=\"checkbox\" class=\"rcbCheckBox\">Smith,Jasmin</li>" +
"<li class=\"rcbItem \"><input type=\"checkbox\" class=\"rcbCheckBox\">SmithJasmin</li>" +
"</ul></div>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
ArrayList<String> innerHTML = new ArrayList<>();
while (matcher.find()) {
String html = matcher.group(0);
html = html.substring(1, html.length()-1);
System.out.println("Full match: " + html);
// Now you have all the value in between tags in this ArrayList called innerHTML
innerHTML.add(html);
}
Full match: Check All
Full match: Dallas , Wanda
Full match: Parker , Tina
Full match: Richardson , Christina
Full match: Dave , Test
Full match: clinician , test
Full match: Smith , Jasmin
现在,您可以通过迭代innerHTML
ArrayList来访问这些HTML内容。希望它有所帮助。
答案 5 :(得分:0)
请尝试这个,它会起作用。
List<WebElement> elementList=driver.findElements(By.xpath("//ul[@class='rcbList']/li"));
List<String> valueList=new ArrayList<String>();
int size=elementList.size();
for(int i=0;i<size;i++){
valueList.add(elementList.get(i).getText());
}