Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a requirement in which I gettext from a site and then later on use that in a loop so it can continue to click elements base on that variable. This works but only loops for 5 times and the number of times it should click is like 100 and or above. here is a sample of the code
String vText= driver.findElement(By.xpath(")).getText();
System.out.println(vText);
int vEle= vText.length();
for (int i=0; i<vEle; i++){
driver.findElement(By.xpath("")).click();
What am I doing wrong please help me out
Thanks,
Mediha
You might need to verify locators from where you are taking text 100 times. If it is same for all 100 elements it would work.
Have you tried with findElements method?
//It will return the List of webelements which has same locator
List<WebElement> elements=driver.findElements(By.xpath(""));
//Now iterate through List and do the required operation with each individual element
for(WebElement ele:element)
{
ele.getText(); //It will print innertext of each element
ele.click(); //It will click on each element
}
I think that the error in your example is in this line:
int vEle= vText.length();
And later on your loop is:
for (int i=0; i<vEle; i++){
Which means youl loop is going to happen only that many times as the text is long. So if the text was Hello then the loop will happen only 5 times.
Thanks to Santosh and Pavel,
I actually found another approach although I will look in to the code you sent me Santosh. But here is the code I came up with and please give me comments as to if is this the best approach.
String vText driver.findElement(By.xpath("")).getText();
//This will split the output from the slash and the output looks like 1/120
final String[] splitted = vText.split("\\D+");
//Here it will parse that output to an integer type and start with the first location
final int slideCount = Integer.parseInt(splitted[1]);
for (int i=0; i<slideCount; i++) {
driver.findElement(By.xpath("")).click();
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 days ago.
Improve this question
Say for instance I want to repeat the line of code
Integer int1 = new Integer(value1);
for many variables, such as int1 to int100. I am not asking about this exact task in particular - I am asking about any such situation where the code would be identical save for replacing small details like int1 and value1 with int2, value2. Is there a way to have the JVM complete this for me?
I am not even sure what approach to take on this or what term to search for more information. The only thing I can think to try is instead of typing "int1", having a loop that changes a string containing the name and attempting to pass the string as a symbol to the JVM but this of course does not work.
It was a little strange question and I don't know if I understood your meaning correctly or not.
But in this particular case, instead of repeating the code, you can use a data structure like an array. See Oracle tutorial.
int[] numvar = new int[arr.length];
for (int i = 0; i < args.length; i++) {
int someNumber = Integer.parseInt(args[i]);
numvar[i] = someNumber;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So I'm programming a Go Fish game for a project of mine, and I've been at this for a while. Currently I'm trying to check if the player/computer has a full book of a certain number. I've based the whole game off of arrays, and just need to check if the array contains four instances of a given element.
I'm not sure about the specifics but this should get you started.
int count = 0;
for(int i = 0; i < YOURARRAY.length; i++)
{
if( //check to see if the element is the type youre counting)
{
count++;
}
}
if(count == 4)
{
//there are 4 instances
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
What is the main use of for loop in Automation testing(Selenium Webdriver?
You would use the for loop for iteration over a collection of objects. Number of methods in Selenium return a collection of objects. You would iterate over them with the for loop and perform necessary logic within the loop body.
For example WebDriver has the following method: java.util.List<WebElement> findElements(By by)
You can use the for loop to iterate over a List of returned WebElements and perform the necessary actions within the loop body.
A small example of for loop:
// Line below returns a `List` of `WebElements`
List<WebElement> rows = driver.findElements(By.xpath("//table/tr"));
// For loop follows
for (int i = 0; i < rows.size(); i++) {
// Access individual elements this way:
WebElement currentRow = rows.get(i);
// Perform an action with currentRow here
}
I'm making a quiz game with 3 different choices. First, I created a list with a total of 17 questions and answers. Then to make sure that the order is ramdomized every time I play the game I will Collections.shuffle(MyList) the whole list at the beginning.
My structure:
I'm picking a question at a randomized position in the list. But, the question is randomized between 0-2, so if I get "1", I will get the question like this Mylist.get(1).getQuestion().
The choices/answers are at the position 0-2 in the list. (The first choice will be: Mylist.get(0).getAnswer(), the second Mylist.get(1).getAnswer() and the third Mylist.get(2).getAnswer()) By doing this, one of the answers will be correct since the index of the question is also between 0-2. And to keep the right choice in a randomized position.
However, the problem is, every time I answer a question, this question will be removed from the list. (Mylist.remove(1)) So, in the end, there will be less than 3 items left and I won't have 3 unique answers. My goal is to have 3 different choices with randomized order on every question, and one of them is, of course, correct. I'm new into programming and don't really know how to fix it. (Everything is in Netbeans).
Thanks in advance!
So you have 17 questions and each question has a correct answer, and you need to show each of these questions with three proposed answers, one of which is correct and other two are wrong (but these are correct answers to other questions)?
Shuffle the list only once, at the beginning of the program. Then, use a for loop to present the questions in order. Don't remove answered questions; the loop will ensure that each question is asked only once. Since the list is shuffled, the user will not be able to predict questions.
Then, fill in the proposed answers: put the correct answer at a random position, and fill the remaining positions with random other answers (use the while loop to ensure that these answers are different from the correct one and from each other).
Something like:
Collections.shuffle(MyList);
for(int i=0; i<MyList.size(); i++){
System.out.println(MyList.get(i).getQuestion();
Answer[] answers=new Answer[3];
int correct=(int)(Math.random()*3);
answers[correct]=MyList.get(i).getAnswer();
int j=i, k=i;
while( j==i ){
j = (int)(Math.random()*MyList.size());
}
answers[(correct+1)%3] = MyList.get(j).getAnswer();
while( k==i || k==j ){
k=(int)(Math.random()*MyList.size());
}
answers[(correct+2)%3] = MyList.get(k).getAnswer();
System.out.println("Answers:");
for(int t=0;t<3;t++){
System.out.println(answers[t]);
}
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I had recently a discussion about the use of non-counter related conditions in for-loops in Java:
for(int i = 0; o.getC() < 10; i++)
o.addC(i);
Does anyone know if there are any "official" conventions for for-conditions like this? In my opinion it's easier to read compared to an equivalent while-loop because all loop-parameters are together in the first line:
int i = 0;
while(o.getC() < 10) {
i++;
o.addC(i);
}
Or even worse:
int i = 0;
while(o.getC() < 10)
o.addC(++i);
for loops are used in pretty much every situation over equivalent while solution. Arrays, lists, standard data structures.
On the other hand while is commonly used with streams and for infinitely long iterations..
Most developers will expect a for statement to consist of three things:
Initialization of a variable
Termination condition based on the variable
Increment on the variable
If you change your code to contain unexpected things it will get harder to read and thus harder to maintain.
Furthermore, I think the while loop makes your intention clearer: do something while o.getC() is less then 10. This "something" happens to be: add an incrementing number.
Long story short: use a while loop for "non-counter related conditions".
A nice thing about for loops is there is a shortcut method. So if you want to do all of the items in an array you can just do the following:
(double number : arrayName)
where double is the type, number is the name you are giving each element (it doesn't matter really what you call it, you will refer to each value there as "number" in this case). And arrayName is the name of the array you are referring to.
If you want to reach each element/object this is the fastest way.
How about:
for(int i = 0; i < MAX_ITERATIONS; i++)
{
o.addC(i);
if (o.getC() >= 10)
return o; // or break
}
This prevents an infinite loop if addC is not really doing what you expect.