java selenium how to use if else - java

There is a table with application numbers and user data. at the beginning of the table, the application number at the end is a green arrow. There can be an infinite number of fields in a table and it always changes.
I need to find the green arrow by the application number and click on it accordingly.
tried to do something like
String myorder = "629/0000/000000021059";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div/div[1]/table/tbody/tr/td[5]\n")).getText())){
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[1]/table/tbody/tr/td[18]/a/img\n")).click();
} if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/table/tbody/tr/td[18]/a/img\n")).click();
} if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[3]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[3]/table/tbody/tr/td[18]/a/img\n")).click();
and so on up to 100+
The first problem is that I really don't like the option of writing if else 100 times. And there is only one digit in the diva where the id of the application and one in the digit in the diva where the green arrow is different(img)
The second problem is that no matter how I insert break; when finding the right application, he clicks on the desired arrow, but after apparently trying to continue searching the page, but the page has already changed and the error falls
(WARNING: The server did not provide any stacktrace information)
if I use the code that was attached above or just an indication of the element (which goes right after the one that turned out to be correct and the page has changed) with a note that I can not find it, if I use else if with break;
tried to do so
for (int i = 1; i < 25; i++) {
String myorder = "629/6300/000000412067";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div["+ i++ +"]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div["+ i++ +"]/table/tbody/tr/td[18]/a/img\n")).click();
break;
}
But if the item is located, then it clicks on the very first arrow in the table, and not on the one in the same column as the application number
I would really appreciate your help!

XPaths are 1-based not 0, so you are correct in thinking you need to add one, however, these two are not equal as i++ modifies the value of i (even inline like you have it), which you do twice inside your loop. Therefore the 2nd iteration of your loop i will be 3, not 1 as expected. In your loop replace i++ with i + 1 and you should be good to go.

for (int i = 1; i < 25; i++) {
String myorder = "629/3500/000000329976";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[" + i + 1 + "]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[" + i + 1 + "]/table/tbody/tr/td[18]/a/img\n")).click();
break;
}
}
Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[31]/table/tbody/tr/td[5]
"}
So it was searching untill 31. And it's strange because i used for i < 25 and there are only 25 rows in the table.
but still dont work for me;(

Related

How do I select a specific element from a set with similar XPath paths?

There are 2 drop-down lists. Each has a similar meaning, for example, "Jorge". Lists in different modules. When I need to fill in, for example, a list that is lower in the tree, then the first match is taken along the XPath path, on an undisclosed list.
Not lists, but values in drop-down lists!
There are 2 drop-down lists. Each has a similar meaning, for example, "Jorge". Lists in different modules. When I need to fill in, for example, a list that is lower in the tree, then the first match is taken along the XPath path, on an undisclosed list.
Not lists, but values in drop-down lists!
I wanted to implement it in Java this way:
Example:
if (findElement(By.xpath("(//example//example)")).isDisplayed()) {
findElement(By.xpath("(//example//example)")).click();
}
But in this case, the element is not displayed.
How to implement a search of all values similar to the XPath path in order to get the one that is displayed?
I tried to do something like this: (//example//example)1 (//example//example)[2] (//example//example)[3]
In my case, we have that 1 - the element does not exist [2] - exists, but is not displayed (isDisplayed = false) [3] - exists, is displayed (isDisplayed = true)
iterating through the values in the loop for [n] cannot be implemented, because, for example, the value 1 is not.
Described as difficult as possible :D. Excuse me.
If someone understands my nonsense, please help me. How to implement my requirement?
enter image description here
UPD:
The problem was solved (for me) by substituting the first value into the expression ()"{1}" immediately.
Now I'm interested in why I get an exception after the first iteration:
Method threw 'org.openqa.selenium.ElementNotInteractableException' exception.
Code:
int number = 1;
String option = "(//ul[contains(#style, 'display: block')]//li//span[contains(text(),'" + valueField + "') or strong[contains(text(),'" + valueField.toUpperCase() + "')]])";
findElement(By.xpath(option+"["+number+"]"));
String[] words = valueField.split(" ");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < words.length; i++) {
builder.append(words[i]);
setFieldByLabel(nameModule, nameLabel, builder.toString());
fastWaitLoading();
for (int y = 0; y < 10; y++) {
if (findElement(By.xpath(option+"["+number+"]")).isDisplayed()) {
new Actions(browser.getWebDriver())
.moveToElement(findElement(option))
.click()
.build()
.perform();
break;
}
number++;
}
}
So I am trying to fully understand your question, and I don't. What I would recommend for a situation like this is, iterate through all elements by creating a list with: findElements(By.xpath ... )
This way you will get a list of webelements and you can iterate through them. Then apply a foreach, assert if element is displayed (it exists as it has been found with findElements) and you should be able to interact with it.
Yeah, everything is in a prominent place)
Missed it
new actions(browser.getWebDriver()) .moveToElement(findElement(**option**)) .click() .build() .perform(); break;
Here
new actions(browser.getWebDriver())
.moveToElement(findElement(**option + "[" + number+"]"**))
.click()
.build()
.perform();
break;

Is there a function in Java to delete indexes which contain 3 data directly in one line?

I want to delete an index that consists of data: name, pass, and phone. At first, I used array.remove(index) method but it only worked if I only have one data. I have tried to use some formula too such as array.remove((index-1)*3) but when I chose show data, it all got messed up. I tried to use looping too but the end result also got messed up.
Is there any way or logic that I can implement to do that?
and this is my current code :
case 3:
show(menu);
System.out.println("Input data number to be deleted: ");
int index = scan.nextInt();
//menu.remove(index);
//menu.removeRange
for(int i = index-1, j = index ; i < menu.size(); i+=3, j++){
menu.remove(i);
menu.remove(j);
//System.out.println("Data is removed");
}
// menu.remove((index-1)*3);
// menu.remove((index-1)*3+1);
// menu.remove((index-1)*3+2);
System.out.println("Data is removed");
break;
}
It's a bit hard to work out what you are trying to do but I'm guessing your issue is that you are removing items from a collection using an index and ignoring the fact that removing one item moves the subsequent items forward.
In other words,
collection.remove(3);
collection.remove(3);
collection.remove(3);
Removes items that were originally at indices 3, 4 and 5

How to alter and display only one column (via user input) on 2D array Java

I am creating a game board. I need it to reveal a the selected column after a user input, while the rest of the columns still print as "X". This game holds values I have set in each column, but does not print them on the screen. When the user selects a column, I need it to print showing the value that column is holding while the rest of the columns still print "X" so they do not reveal what they have. I am new to this, thank you for your help.
This is the function where I think the problem is. If you look, you will see that I have the if statement "if (isCovered) - then I want it to print the covered version. Then the "else" - which is where I want it to print just the one that was guessed as its actual value. I have tried multiple ways of achieving this with no luck. Is there are way to make it like (!isCovered)? But that doesn't work, because it states it needs to be an array and the function "!" does not work. Right now it just seems like it never prints the "else" statement at all. I have functions that take the user input and compare them to "isCovered" and they work correctly, because the piece moves on the board as it should. I just cannot get it to print the actual value instead of an "X". Thank you for any help and if further information would be helpful, please let me know. It is due today unfortunately I only had a few days to work on it and have been working constantly on it.
public static void PrintRevealBoard(int[][] myArray,Boolean[][] isCovered)
{
int i, j;
for (i = 0; i<myArray.length ; i++ ) { // array.length = max rows
System.out.print((i+1) + " ");
for(j = 0; j <myArray[0].length; j++) { // array[0].length = max
cols
if(isCovered[i][j]){
System.out.print(GetRollColorCovered(myArray[i][j]) + " ");
} else {
System.out.print(GetRollColor(myArray[i][j]) + " ");
}
}
your main module is kinda messy. And I don't know how GetRollColor(dice) works. Anyway as I understand you have a two dimensional array and you want to show only a specific value. Seems like u want to show the entire input column.
use this to update isRevealed() after the input of inputCol.
public static Boolean[][] updateRevealed(Boolean[][] isRevealed, int inputCol){
for(int i=0;i<isRevealed[inputCol].length;i++)
isRevealed[inputCol][i] = true;
return isRevealed;
}
update like this,
isRevealed = updateRevealed(isRevealed,inputCol);
your printRevealBoard is almost correct. Just remove the first line. It doesn't make sense and you don't want it as I see
int isRevealed = inputCol;
I don't know how your array looks like. But because of the first for loop u will definitely get an
index out of bounds exception
loop runs until I becomes myarray.length. and in the next loop you access index I of myArray. Exception will be thrown if I=myArray.length. u gotta fix it. If any problem occurs lemme know.
thankyou
edit:
try this for printRevealBoard
public static void printRevealBoard(char[][] myarray , Boolean[] []isRevealed){
for(int i=0;i<myarray.length;i++){
for(int j=0;j<myarray[0].length;j++){
if (isRevealed[i][j]) System.out.print(myArray[i][j] + " ");
else System.out.print("* ");
}
System.out.println();
}
}

Java school project random error

https://www.dropbox.com/s/5iklxvhslh4kfe7/CS%203114.zip
There's some bug in my code for my school project that I just can't figure out. The link above is to my code for the project. The project instructions is in the P1.pdf file.
My error has something to do with this code:
/*
for (int i = 0; i < reactions.length; i++)
{
reactions[i].UpdateFireTime();
debugwriter.write(i + "| " + reactions[i].FireTime());
debugwriter.newLine();
}
debugwriter.newLine();
heap.build();
//*/
//*
for (int i = 0; i < table[reactionIndex].length; i++)
{
int rindex = table[reactionIndex][i];
reactions[rindex].UpdateFireTime();
}
for(int i = 0; i < reactions.length; i++)
{
debugwriter.write(i + "| " + reactions[i].FireTime());
debugwriter.newLine();
}
debugwriter.newLine();
heap.build();
//*/
The first for loop updates the firing time of every reaction, while the second for loop uses my table to update specific dependent reactions. My answers are correct for the first for loop but incorrect when I use the second one. I've tested to see which propensities change if I update every reactions firing time and the results match my table. This means the only difference is the -Math.log(Math.random()) factor. If I set the random number to a constant, then I get the same results using both loops. I've looked over my code many times and I just can't figure out what the problem could be. Can anyone help me out?
P.S.:
The .ltf files are just .txt files that are quite large. I use the .ltf to distinguish them from regular .txt files
The correct means for the DIMER example are: ~650 ~650 ~220
EDIT: The third loop is just for debugging purposes. The 2 loops I'm talking about are the 1st and 2nd one where the 1st one is the one that's commented out.
You don't need table[reactionIndex] in your first loop. Just use table.length-1 and you can use i as your spot in the index to loop through and do stuff with.

Checking if ArrayList element exists or not

I'll try to explain this as best I can. I have an ArrayList of String's. I am trying to implement server-side paging for a webapp. I am restricted to the number of items per page (6 in this case) which are read from this ArrayList. The ArrayList is, lets say, the entire catalog, and each page will take a section of it to populate the page. I can get this working just fine when there are enough elements to fill the particular page, its when we hit the end of the ArrayList where there will be less than 6 items remaining for that pages segment. How can I check if the ArrayList is on its last element, or if the next one doesn't exist? I have the following code (in pseudo-ish code):
int enterArrayListAtElement = (numberOfItemsPerPage * (requestedPageNumber - 1));
for (int i = 0; i < numberOfItemsPerPage; i++) {
if (!completeCatalog.get(enterArrayListAtElement + i).isEmpty() {
completeCatalog.get(enterArrayListAtElement + i);
}
}
The if in the code is the problem. Any suggestions will be greatly appreciated.
Thanks.
It sounds like you want:
if (enterArrayListAtElement + i < completeCatalog.size())
That will stop you from trying to fetch values beyond the end of the list.
If that's the case, you may want to change the bounds of the for loop to something like:
int actualCount = Math.min(numberOfItemsPerPage,
completeCatalog.size() - enterArrayListAtElement);
for (int i = 0; i < actualCount; i++) {
// Stuff
}
(You may find this somewhat easier to format if you use shorter names, e.g. firstIndex instead of enterArrayListAtElement and pageSize instead of numberOfItemsPerPage.)
Can't you just get
completeCatalog.size()
and compare it to i? i.e to answer the question "is there an ith element" you say
if (i<completeCatalog.size())
You just need to add a second expression to look whether the end of the list was reached already:
int enterArrayListAtElement = (numberOfItemsPerPage * (requestedPageNumber - 1));
for (int i = 0; i < numberOfItemsPerPage; i++) {
if (enterArrayListAtElement + i < completeCatalog.size() && !completeCatalog.get(enterArrayListAtElement + i).isEmpty() {
completeCatalog.get(enterArrayListAtElement + i);
}
}
An ArrayList has the method of size(), which returns the number of elements within the List.
Therefore, you can use this within the if statement to check you've not went too far.
For example,
if(enterArrayListAtElement + i < completeCatalog.size()) {
...
}

Categories