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);
Related
running into a problem.
The code is not finding the string that equals 8QQ which is a match
here is my full code.
String selector = "div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a";
int links = driver.findElements(By.cssSelector(selector)).size();
System.out.println("Number of links: " + links);
// begin inner for-loop
for (int i = 0; i < links; i++) {
PP_OBJ_CycleData.ScrollToTop(driver);// scroll up
List<WebElement> CCTable = driver.findElements(By.cssSelector(selector));
WebElement code = CCTable.get(i);
System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n");
//code.click();
//----------------------checking for bad code -----------------------------------------
String[] badcodes = {"8QQ", "8BQ", "8JQ"};
boolean check = Arrays.asList(badcodes).contains(code);
System.out.println(check);
//check == true will work as well
if(check){
System.out.println(check+"Bad Code found breaking loop");
break;
}else{
//checking to make sure element is clickable
PP_OBJ_CycleData.isClickable(code, driver);
System.out.println("Clickable?"+ code.isEnabled());
code.click();
}
I think the problem is this part here:
List<WebElement> CCTable = driver.findElements(By.cssSelector(selector));
WebElement code = CCTable.get(i);
System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n");
//code.click();
I am cutting off everything but the first 3 characters with sub string but that is during the print statement. however the string is actually longer or has more characters than what is in the array for 8QQ. Could this be the problem if so is there a way to get around it? So that it only tries to match the first 3 characters? the.contains() is not doing it i don't think.
I saw matches.find() methods but have not had any success.
This is my code now it works had to think about it for a long time my code is below. and now it breaks loop.
String selector = "div#companyIdBarCompListGrid_rows_scrollpane table tbody tr[id*=companyIdBarCompListGrid_] td span div a";
int links = driver.findElements(By.cssSelector(selector)).size();
System.out.println("Number of links: " + links);
// begin inner for-loop
for (int i = 0; i < links; i++) {
PP_OBJ_CycleData.ScrollToTop(driver);// scroll up
List<WebElement> CCTable = driver.findElements(By.cssSelector(selector));
WebElement code = CCTable.get(i);
// changing code to mcode to look for first 3 characters
String mcode = code.getText().substring(0, 3).trim();
System.out.println("\n"+ "modified "+ mcode);
// System.out.println("\n"+code.getText().substring(0, 3).trim()+"\n");
//----------------------checking for bad code -----------------------------------------
String[] badcodes = {"8QQ", "8BQ", "8JQ"};
boolean check = Arrays.asList(badcodes).contains(mcode);
System.out.println(check+"code in array list");
//check == true will work as well
if(check){
System.out.println(check+"-Bad Code found breaking loop");
break;
}else{
//checking to make sure element is clickable
PP_OBJ_CycleData.isClickable(code, driver);
System.out.println("Clickable?"+ code.isEnabled());
code.click();
}
There are two problems with your code
Arrays.asList(badcodes) returns a List<String>, while code is a WebElement: you're comparing two different types, so check will never be true
You say that code is more than three characters, but List.contains expects equality.
You might be confusing String.contains and List.contains.
myString.contains(code) will return true if code is a substring of the String myString
myList.contains(code) will return true if code is an element of the List myList
In your code, Arrays.asList(badcodes).contains(code) calls List.contains, and returns false because - as you say - code is longer than any of the Strings you're comparing to.
How do you fix it?
Convert code to a String. You should know how to do that, because you already do it in the System.out.println above.
Ensure code has the same length as the strings you're comparing to. You should know how to do that, because you already do it in the System.out.println above.
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.
I am writing a simple Java program that basically stores an array of artists that have been in the charts previously; this is my code so far for the program
package searching;
import java.util.*;
public class Searching {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String artists[] = {"Rihanna", "Cheryl Cole", "Alexis Jordan", "Katy Perry", "Bruno Mars",
"Cee Lo Green", "Mike Posner", "Nelly", "Duck Sauce", "The Saturdays"};
System.out.println("Please enter an artist...");
String artist = scanner.nextLine();
}
}
I was just wondering, is it possible for the user to type the name of one of the artists, get the code to search the array and return the index of that value? and if so how would I go about it as I don't know where to begin... thanks in advance!
With an unsorted array, one option would be to put the artists in a List and use List.indexOf().
List<String> artistsList = Arrays.asList( artists );
...
int index = artistsList.indexOf( artist );
If the artists were sorted, you could use Arrays.binarySearch().
You need to loop through the artists array in a for loop and then return the index if the value equals the artist value.
for (int i = 0; i < artists.length; i++) {
String artistElement = artists[i];
if (artistElement.equals(artist)) {
System.out.println(i);
}
}
Here's what happened for me:
Please enter an artist...
Mike Posner
6
I was just wondering, is it possible for the user to type the name of one of the artists, get the code to search the array and return the index of that value?
Yes, it is possible.
Since you don't know where to begin, I would say you can start going through the array (probably using a for loop) and validating if the artist variable is equals to the current element of the array. If they're equals, then you can just return the current index of the element of the array. If nothing is found, then return a default value like -1 that you can handle and return a message like Artist not found.
You could do it like so:
int index = -1;
for (int i = 0; i < artists.length; i++) {
if (artist.equals(artists[i]))
index = i;
}
if (index == -1)
System.out.println("Artist not found");
else
System.out.println("Index of artist: " + index);
}
This isn't as eloquent as tieTYT's solution, but does the trick. The index is set to -1. The for loop compares each artist to each value in your array. If a match is found, the index is set to the index of the element.
After the for loop, if the index is still -1, the user is informed that no match was found, otherwise the appropriate artist and index are output.
The user of a for loop is the most common way to scroll through the contents of an array and compare elements against a given value. By calling artists[i], every element of the array can be checked against the input String.
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)));
}