How to capture search result count.It varies each time I had tried with
int result = driver.findelements(by.xpath("")).size(); but always I'm getting 0. XPath is correct.
Code in website:
<div class="search-msg-count ng-binding">Search result count: 100 entries</div>
Capture the text from the Element with the xpath -
WebElement e = driver.findElement(By.xpath("//div[#class='search-msg-count ng-binding']"));
String elementText = e.getText().trim();
Then split the string with ":" -
String split[] = elementText.split(":");
Your required text will be in second array element. Get it & replace entries word from it.
String finalValue = split[1].replace("entries","").trim();
System.out.println(finalValue);
Related
I have an Array of 10 strings:
so now when i get the InnerText from the object,
i want to trim off the last characters of all the strings within the Array
I have -->
println(Array)
Array =
[User RolesPDF, Create New UserPDFVideo, Create Self-Registration CodePDF, User ManagementPDF, Email SettingsPDF, Download RFIsPDF, Manage Departments & Setup HierarchiesPDFVideo, Tracer Category SetupPDFVideo, Guest Access SetupPDF]
I want to get rid of the PDF (3 characters so the pDF at the end is removed)
Also - Video (8 characters removed)
def Array = []
WebUI.comment('Top of Page - For loop for all Text of pdf and videos, and also clicking on all PDFs')
for (i = 1; i < 10; i++) {
WebUI.delay(1)
WebUI.scrollToPosition(80, 80)
String innertext = driver.findElement(By.xpath(('//div[#class=\'content-sections\']/div[1]//div[#class=\'col-12 col-xl-6\']/div[' +
i) + ']//div[#class=\'item\']')).getText()
println(innertext)
Array << innertext
I have tried:
def Arraysliced =Array.size
for (int i = 0;i<Array.size;i++){
Arraysliced[i].substring(0)
println(Arraysliced);
}
String delims = "[ ]+";
String Arraysliced = Array.split(delims)
Arraysliced = Array
def Arraysliced = []
Arraysliced = Array.split('-'[0])
(Array.split('-')[0])
println(Arraysliced)
Can you try below code-
String [] arrsplit = innertext.split("//PDF");
and use arrsplit[0] to add in your code
> **Output would be**- if innertext is- Create New UserPDFVideo
> arrsplit[0]=Create New User arrsplit[1]=Video //which you need to ignore.
String str = "some value" ;
getTestBase().getDriver().findElement(By.xpath("//h2[.='" + str + "']//parent::div//div[#id='sectionList'][1]/section/div/button")).click();
I need to put the above in loop so that [1] keeps increasing every time , how can I update the above xpath for the integer ?
String str = "some value" ;
WebDriver driver = getTestBase().getDriver();
for(int i=0; i<5; i++){
driver.findElement(By.xpath("//h2[.='" + str
+"']//parent::div//div[#id='sectionList']["+i+"]/section/div/button")).click();
}
This should solve your problem. 5 is just an arbitrary limit i assumed for writing the solution.
On the other hand you can try and get all the elements and store the elements in a list. Then parse the list to get the the buttons and click on them.
Some thing like:
String str = "some value" ;
WebDriver driver = getTestBase().getDriver();
List<WebElement> listOfButtons = driver.findElements(By.xpath("//h2[.='" + str + "']//parent::div//div[#id='sectionList']//section/div/button""));
for(WebElement el: listOfButtons){
el.click();
}
Actually, I'm downloading HTML code with iON library and in this HTML page there is just a string like |5.00| or it can be |10.00|. So how can I get the value that is in middle of |?
The problem is that I don't have the exact string |10.00| but all the HTML code so I have to use something like if(htmlstring.contains("|").
But then, I can't get how to get the value from that |.
You can use indexOf()
String htmlstring = "abcd|10.0|1234";
int firstIndex = htmlstring.indexOf('|');
int lastIndex = htmlstring.indexOf('|',firstIndex+1);
String value = htmlstring.substring(firstIndex+1, lastIndex);
Try :
String val = "|10.00|";
String output[] = val.split("\\|",val.length());
val = output[1].substring(0,val.length()-2);
How can I search text in HTMLDocument and then return the index and last index of that word/sentence but ignoring tags when searching..
Searching: stackoverflow
html: <p class="red">stack<b>overflow</b></p>
this should return index 15 and 31.
Just like in browsers when searching in webpages.
If you want to do that in Java, here are rough example using Jsoup. But of course you should implement the detail so that the code can parse properly for any given html.
String html = "<html><head><title>First parse</title></head>"
+ "<body><p class=\"red\">stack<b>overflow</b></p></body></html>";
String search = "stackoverflow";
Document doc = Jsoup.parse(html);
String pPlainText = doc.body().getElementsByTag("p").first().text(); // will return stackoverflow
if(search.matches(pPlainText)){
System.out.println("text found in html");
String pElementString = doc.body().html(); // this will return <p class="red">stack<b>overflow</b></p></body>
String firstWord = doc.body().getElementsByTag("p").first().ownText(); // "stack"
String secondWord = doc.body().getElementsByTag("p").first().children().first().ownText(); // "overflow"
//search the text in pElementString
int start = pElementString.indexOf(firstWord); // 15
int end = pElementString.lastIndexOf(secondWord) + secondWord.length(); // 31
System.out.println(start + " >> " + end);
}else{
System.out.println("cannot find searched text");
}
String k= <html>
<a target="_blank" href="http://www.taxmann.com/directtaxlaws/fileopencontainer.aspx?Page=CIRNO&
amp;id=1999033000019320&path=/Notifications/DirectTaxLaws/HTMLFiles/S.O.193(E)30031999.htm&
amp;aa=">number S.O.I93(E), dated the 30th March, 1999
</html>
I'm getting this HTML in a String and I want to remove the anchor tag so that data is also removed from link.
I just want display it as text not as a link.
how to do this i m trying to do so much not able to do please send me code regarding that i m
creating app for Android this issue i m getting in android on web view.
use JSoup, and jSoup.parse()
You can use the following example (don't remember where i've found it, but it works) using replace method to modify the string before showing it:
k = replace ( k, "<a target=\"_blank\" href=", "");
String replace(String _text, String _searchStr, String _replacementStr) {
// String buffer to store str
StringBuffer sb = new StringBuffer();
// Search for search
int searchStringPos = _text.indexOf(_searchStr);
int startPos = 0;
int searchStringLength = _searchStr.length();
// Iterate to add string
while (searchStringPos != -1) {
sb.append(_text.substring(startPos, searchStringPos)).append(_replacementStr);
startPos = searchStringPos + searchStringLength;
searchStringPos = _text.indexOf(_searchStr, startPos);
}
// Create string
sb.append(_text.substring(startPos,_text.length()));
return sb.toString();
}
To substitute all the target with an empty line:
k = replace ( k, "<a target=\"_blank\" href=\"http://www.taxmann.com/directtaxlaws/fileopencontainer.aspx?Page=CIRNO&id=1999033000019320&path=/Notifications/DirectTaxLaws/HTMLFiles/S.O.193(E)30031999.htm&aa=\">", "");
No escape is needed for slash.