I have a code that retrieves the URL's of a webpage. I wanted to know if it is possible to convert this to a String or obtain a String from the System.out.print() method?
for(WebElement link : links)
System.out.println(link.getAttribute("href"));
Edit1: I would like to create an array of strings obtained from the above code if it is possible.
I would like to copy the codes output information and use it in an email for only 15 items:
for(int x = 1; x < 15 ; x++) {
String url = link.getAttribute("href");
messageBodyPart.setText(url);}
If you want to have an array list you can have it as:
List<String> urls = new ArrayList<String>();
for(WebElement link : links){
urls.add(link.getAttribute("href"))
}
And then use it by fetching list elements in your java mail.
Related
I have to verify the tool tip message displayed in a table using selenium web driver.I have taken the actions class and used build.perform() but it returns an empty string. The problem is , html code is in such a way that the tool tip tags are incorporated with the span tag. Please let me know how to resolve this issue.
HTML code :
<span class="abcd" data-qtip="<table><tr><td>xxx as of 05/04/2017 </td><td>$1.00</td></tr><tr><td>yyy</td><td>$1.00</td></tr><tr><tdzzz</td><td>$0.00</td></tr><tr><td>xyz</td><td>0.00%</tr></td></table>"/>
xxx,yyy,zzz and xyz are the texts I need to verify.
Method I used:
public void mouseOver(String loc) {
Actions ToolTip1 = new Actions(driver);
WebElement pr = findElement(loc);
System.out.println(pr);
pause(2000);
ToolTip1.moveToElement(pr).perform();
pause(2000);
String toolTipMsg = pr.getText();
pause(1000);
System.out.println(toolTipMsg);
}
where loc = xpath of the element.
Any help on this is appreciated.
After doing mousehover to the webelement, use the getAttribute method to get the value of "data-qtip". Below line can help you.
String toolTip = driver.findElement(By.xpath("//span[#class='abcd']")).getAttribute("data-qtip").toString();
Store the expected message in another string and compare that with toolTip.
Since in this case, the string toolTip is in Html and we need to parse it to required format and compare it to the expected string. The below code parses the toolTip and gives the String which needs to be validated.
System.out.println(toolTip);
String[] word1 = toolTip.split("<td>");
String a = word1[1].split(" ")[0];
System.out.println(a);
String b = word1[3].split("</")[0];
System.out.println(b);
String c = word1[6].split("</")[0];
System.out.println(c);
String d = word1[4].split("<td")[1].split("<")[0];
System.out.println(d);
We can even parse it a better way but this works. Later validate a, b, c, d with the expected messages. Thanks
try this:
Actions ToolTip1 = new Actions(driver);
WebElement pr = findElement(loc);
System.out.println(pr);
ToolTip1.moveToElement(pr).perform();
pause(2000);
String toolTipMsg = driver.findElement(by.cssSelector("span.abcd")).getAttribute("data-qtip");
System.out.println(toolTipMsg);
I was looking for a better solution and i came up with this.
Step 1: Store the result in a string,
String Tooltip = driver.findElement(By.xpath("//span[#class='abcd']")).getAttribute("data-qtip").toString();
Step 2: Use some regex pattern to get the data only from "td" tags and add it to a ArrayList.
ArrayList<String> lists = new ArrayList<>();
Pattern p = Pattern.compile("(<td>(.*?)<\\/td>)");
Matcher m = p.matcher(toolTip);
while(m.find())
{
String tag2 = m.group(1);
String tag = tag2.substring(4,(tag2.length()-5)); //removes the <td> and </td> tags in the matched string
lists.add(tag);
System.out.println(tag);
}
Now the lists will have all the table data's. Iterate through the list elements and compare it to the expected String. Thanks.
Automation scenario is to get all the links in weblist & then get this arraylist content outside loop and then get the index number dynamically based on the url hit.
String values="";
List<WebElement> url_link = driver.findElements(By.cssSelector(".anomaly>a"));
for ( WebElement we: url_link) {
String temp = values;
values = temp + we.getText();
}
System.out.println("Text "+values);
int ind=values.indexOf("www.test.com");
System.out.println("Index "+ind);
The above code returns me a weird index number of 74.
The url_link output contents are:
wwww.hatch.com
wwww.tist.com
wwww.helix.com
wwww.patching.com
wwww.seyh.com
wwww.test.com
wwww.toast.com
wwww.telling.com
wwww.uity.com
so based upon the expected result i am expecting the index number to be 5.
Any help will be appreciated.
The variable values is a variable of type String, it's not a List. So when you do values = temp + we.getText();, you are basically appending all the links in a single String. So the output will actually be this:
wwww.hatch.comwwww.tist.comwwww.helix.comwwww.patching.comwwww.seyh.comwwww.test.comwwww.toast.comwwww.telling.comwwww.uity.com
The index of www.test.com is 74 in this string. You should be adding these links in an ArrayList and then find the index of the link you want to search.
Something like this should work:
List<String> values = new ArrayList<String>();
List<WebElement> url_link = driver.findElements(By.cssSelector(".anomaly>a"));
for ( WebElement we: url_link) {
values.add(we.getText());
}
int ind = values.indexOf("www.test.com");
System.out.println("Index "+ind);
I have string from which I am getting data in array but I am not getting way to get so that I get related data in array like my string is,
[{"Class Room": ["Windows", "Windows1"], "Staffroom": ["Windows", "Windows1"]}], "
here i have two dropdowns ,
"Class Room" having two values in dropdown Windows and Windows1
another drodown,
"Staffroom" having two values in dropdown Windows and Windows1
How would i get dropdown related value in array i can use split operation but cant getting logic
Above given string is in list, my code is ,
String[] data = list.toString().split(",");
But it split in array then I would not have related data
I want it to split in way so that I would get relative dropdown data in array,
"Class Room": ["Windows", "Windows1"], value in aray index 0
"Staffroom": ["Windows", "Windows1"]}] value in array index 1
String is not fix it is generating on run time number of dropdown and values of dropdown vary time to time but pattern of string same as mentioned above
This depends on the UI framework that you are working. If you are using Swings then you have to use ActionListener to display related data. For example
private String s1[] = { "None", "J2EE", "DataBase", "Scripting Language",
"Computer Networks" };
private String s2[][] = { { "None" }, { "Core Java", "Advanced Java" },
{ "Oracle", "SQL", "SyBase" }, { "Java scripts", "c#", "CGI" },
{ "MCSE", "CCNA", "CCNP", "CCIE" } };
...
skill = new JComboBox(s1);
specificSkill = new JComboBox(s2[0]);
...
skill.addActionListener(new ComboAction());
...
specificSkill.setSelectedIndex(0);
You can use this code which can solve your problem as it is tested and verified.
String temp = "[{"Class Room": ["Windows", "Windows1"], "Staffroom": ["Windows", "Windows1"]}],";
String parts[] = temp.split(",");
ArrayList<String> listItems = new ArrayList<String>();
for (int i = 0; i < parts.length; i =i+2) {
listItems.add(parts[i]+","+parts[i+1]);
}
Or you can use regex to break the string at a particular occurence of a character.
I hope it helps.
I have an ArrayList that is built from posts in a forum, each entry is a post.
When I am building the entry I am replacing all elements with the string "Image[x]".
After the array list is filled I want to go back and replace all instances of "[x]" with an incrementing integer.
I am building my array list using this code:
Elements wholePosts = doc.select("div.post_body");
for (Element wholePost : wholePosts) {
Elements texts = wholePost.select("div[itemprop=commentText]");
for (Element text : texts) {
String nobr = text.html().replaceAll("(?i)<br[^>]*>", "newlineplaceholder");
String formatted1 = nobr.replaceAll("<img src=.*?>", "Image[x]");
Document finalPost = Jsoup.parse(formatted1);
String almostfinalText = finalPost.text();
String finalText = almostfinalText.replace("newlineplaceholder", "\n");
datumList.add(finalText);
}
I have tried to replace the string and increment it in the above code but it only increments for each post element, so if there are multiple images in a post, post 1 would contain "Image1, Image1" and post 2 would contain "Image2, Image2". What I am looking for is for post 1 to contain "Image1, Image2" and post 2 to contain "Image3, Image4"
I have two arrays and i want to access data of same index value from other array.
Two Array list :
ArrayList<Integer> Position = new ArrayList<Integer>();
ArrayList<String> List_Data = new ArrayList<String>();
Now my Position array contains Integer value like index of data i.e 0,3,5 out of 10 Records.
i want to get only those string whose index should be i.e 0,3,5 out of 10 .
Example :
String Array >> [A,B,C,D,E,F,G,H,J,K];
Index >> Now i am selecting 2 ,5 index data.
Final Output as string >> C,F
So at the end i get actual string from array.
I get this and some other link also but not get exact idea how to do this.
Please anyone help me.
Try this, If I understand what you want correctly (otherwise let me know)
String sr=Lista_Data.get(Position.get(INDEX YOU NEED; EG 1, 5, 1000...))
You can get object from ArrayList using get function. Then you can use it as an index to another ArrayList.
String res = "";
for (Integer pos : Position) {
res += List_Data.get(Position.get(pos));
}
The only thing you need is method indexOf(...) of List.
public String getStringByIndex(Integer index) {
return List_Data.get(Position.indexOf(index));
}
I am not not saying that above code is wrong but its not working according my needs. or i can't handle because of my other code limitation.
Finally, I get as i want like :
for (int i = 0; i < Poisition.size(); i++)
{
System.out.println("Selected Data --->"+ List_Data.get(Poisition.get(i)));
}