Have a scenario that was to collect all the div ids and loop them one by one to complete the iteration. I have done the scenario but it takes more time to pass all the ids.
Can you please suggest how to make it faster.
Below is my code snippet.
List<WebElement> listoftab = driver.findElements(by.xpath(".//*[contains (#id, 'tabZ')]/div/div[1]"));
Thread.sleep(1000);
String clas1 = "tablist";
String clas2 = "tabView";
for(int i =1; i<=110;i++){
boolean present;
try {
driver.findElement(By.xpath(".//*[#id='tabZ"+i+"']/div/div[1]"));
present = true;
if(clas1.equalsIgnoreCase(driver.findElement(By.xpath(".//*[#id='tabZ"+i+"']/div/div[1]")).getAttribute("class"))) {
tabloop:
for(int j=1;j<=15;j++) {
if(clas2.equalsIgnoreCase(driver.findElement(By.xpath(".//*[#id='tabZ"+i+"']/div/div[1]/div["+j+"]")).getAttribute("class"))) {
String ls = driver.findElement(By.xpath(".//*[#id='tabZ"+i+"']/div/div[1]/div["+j+"]")).getAttribute("id");
System.out.println(ls);
driver.findElement(By.xpath(".//*[#id='"+ls+"']/div[1]/div[2]/canvas[2]")).click();
Thread.sleep(3000);
break tabloop;
}
}
}
} catch (NoSuchElementException e) {
present = false;
continue;
}
}
Try this code as you are using driver.findElement() multiple times.Try to avoid finding element instead store them in a variable.
List<WebElement> listoftab = driver.findElements(By
.xpath(".//*[contains (#id, 'tabZ')]/div/div[1]"));
Thread.sleep(1000);
String clas1 = "tablist";
String clas2 = "tabView";
for (int i = 1; i <= 110; i++) {
boolean present;
try {
WebElement element=driver.findElement(By.xpath(".//*[#id='tabZ" + i
+ "']/div/div[1]"));
present = true;
if (clas1.equalsIgnoreCase(element.getAttribute("class"))) {
tabloop: for (int j = 1; j <= 15; j++) {
WebElement element1=driver.findElement(
By.xpath(".//*[#id='tabZ" + i
+ "']/div/div[1]/div[" + j + "]"));
if (clas2.equalsIgnoreCase(element1
.getAttribute("class"))) {
String ls = element1.getAttribute("id");
System.out.println(ls);
driver.findElement(
By.xpath(".//*[#id='" + ls
+ "']/div[1]/div[2]/canvas[2]"))
.click();
break tabloop;
}
}
}
} catch (NoSuchElementException e) {
present = false;
continue;
}
}
Try to avoid hard wait also.Better go with fluentwait.
Related
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 3 years ago.
My select statement shows the error
java.sql.SQLSyntaxErrorException: You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near 'condition from food' at line 1.
My parameter values are object = "food" and columns[] = {"foodName, foodPrice, condition"}
This function still works for my login though
I already use different versions of mySQL and also put brackets around the statement and it still doesn't work
public static String[][] checkDatabase(String object, String[] columns){
Connection myCon = null;
Statement myStm = null;
ResultSet myRs = null;
try {
myCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/softwaredesignorder", "root","SeanLink_11");
myStm = myCon.createStatement();
String temp = "select ";
for (int i = 0; i < columns.length; i++) {
if (i < columns.length - 1) {
temp = temp + columns[i] + ", ";
} else {
temp = temp + columns[i];
}
}
temp = temp + " from " + object;
//PreparedStatement pStm = myCon.prepareStatement(temp);
//pStm.execute();
myRs = myStm.executeQuery(temp);
myRs.last();
String[][] returner = new String[myRs.getRow()][columns.length];
myRs.beforeFirst();
int row = 0;
while (myRs.next()){
int length = columns.length, col = 0;
while (length != 0) {
try {
returner[row][col] = myRs.getString(columns[col]);
} catch (IllegalArgumentException e1) {
try {
returner[row][col] = Integer.toString(myRs.getInt(columns[col]));
} catch (IllegalArgumentException e2) {
try {
returner[row][col] = Double.toString(myRs.getDouble(columns[col]));
} catch (IllegalArgumentException e3) {
try {
Date date = myRs.getDate(columns[col]);
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
returner[row][col] = dateFormat.format(date);
} catch (IllegalArgumentException e4) {
System.err.println("Could not retrieve data");
} catch (Exception e) {
System.err.println(e);
}
}
}
}
col += 1;
length -= 1;
}
row += 1;
}
return returner;
} catch (SQLException exSQL) {
System.err.println(exSQL);
}
return null;
};
I want the output to have the data from the entire table but only the selected column.
I believe your problem is that condition is a reserved work in most SQL languages as detailed in https://dev.mysql.com/doc/refman/5.5/en/keywords.html#keywords-5-5-detailed-C, that is why your code works on some cases, is because you don't always have a column named condition you need to escape it using backticks.
Have you try to output back the query you made? i think this one have issue on your query builder. Just simply System.out.println(temp) and check the query syntax before you query on MySQL.
"conditional" is a reserved word in MySQL. You could avoid this error by surrounding your select list items with quotes:
for (int i = 0; i < columns.length; i++) {
String column = "\"" + colunms[i] + "\"";
if (i < columns.length - 1) {
temp = temp + column + ", ";
} else {
temp = temp + column;
}
}
I have to merge a lot of pdf files, I get base64 coded from a SOAP Action, but the CSV files, from i, read the data for the Protokoll are sometimes not correct. It means, when the n^th data is not correct, it will nothing mergen. I need an option there, to ignore errors if it merges the files.
for(int i = 0; i<bm.size(); i++) {
String wert = leser.getBundlingWert(fileName, qeuecnt, bundling);
new Paket(fileName);
isNewPaket = true;
ut.addSource(Paket.outPutPdf);
if(leser.getPrintMode(fileName, i).equals("SIMPLEX")) {
for(int j = 0; j < bm.get(wert); j++) {
int pf = leser.getPageFrom(fileName, j);
int pt = leser.getPageTo(fileName, j);
seitecnt += (pt - pf) +1;
if(isNewPaket) {
seitecnt = 0;
isNewPaket = false;
}
else if(seitecnt>blattGrenze) {
new Paket(fileName);
ut.addSource(Paket.outPutPdf);
seitecnt = 0;
isNewPaket = false;
}
}
} else if(leser.getPrintMode(fileName, i).equals("DUPLEX")) {
for(int j = 0; j < bm.get(wert); j++) {
int pf = leser.getPageFrom(fileName, j);
int pt = leser.getPageTo(fileName, j);
seitecnt += ((pt - pf) +1)/2;
if(isNewPaket) {
seitecnt = 0;
isNewPaket = false;
}
else if(seitecnt>blattGrenze) {
new Paket(fileName);
ut.addSource(Paket.outPutPdf);
seitecnt = 0;
isNewPaket = false;
}
}
}
qeuecnt += bm.get(wert);
}
try {
ut.setDestinationFileName(path);
ut.mergeDocuments(null);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
If i let this code run and one of the files got data not correctly, it merges nothing, but i need an option, where it ignores the incorrect one.
So, I have a for loop that loops through an array then performs a try/catch to see if any of those indexes can be made a long from their original string value.
for (int i = 0; i < recordLine.split("\t").length; i++) {
try {
long l = Integer.parseInt(recordLine.split("\t")[i]);
} catch (NumberFormatException numberFormatException) {
System.out.println("Here are the Strings: " + i);
System.out.println("NumberFormatException: " + numberFormatException.getMessage());
}
}
String[] splitRecordLineItems = recordLine.split("\t");
Character[] resultsArray = new Character[splitRecordLineItems.length];
for (int i = 0; i < splitRecordLineItems.length; i++) {
try {
long l = Long.parseLong(splitRecordLineItems[i]);
resultsArray[i] = 'L';
} catch (NumberFormatException nfe) {
resultsArray[i] = 'S';
}
}
You can then print the content of resultsArray for example using following line:
System.out.println(Arrays.toString(resultsArray));
I have saved the units in .txt file. These I am getting in an array list. Now I want to check if any of the units present in the string.
List contains :
"units", "kg", "kilogms", "kilo", "literes",
"Liter", "packets", "packet", "gms", "grams", "half kg"
Like, if I have a string - 1kg rice, I want to get numbers from this string and I want to divide this sentence to words and want to compare with each item from array list of units. If it is present I want to save it. So I want to store 1kg and rice separately. string may contain any spaces I want to trim all those spaces and check compare.
Getting text file in an array list.
public class ReadTextFiles {
public static List<String> readItemNamesFile(Context context) {
String sText = null;
List<String> stringList;
try{
InputStream is = context.getResources().openRawResource(R.raw.item_names);
//Use one of the above as per your file existing folder
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
sText = new String(buffer, "UTF-8");
String[] sTextArray = sText.replace("\"", "").split(",");
stringList = new ArrayList<String>(Arrays.asList(sTextArray));
System.out.print(stringList);
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return stringList;
}
}
public void getUnits()
{
List<String> units = new ArrayList<>();
units = ReadTextFiles.readUnitsFile(getActivity());
System.out.print(units.size());
}
Now I want to compare string suppose its "1 kg potato".Then should find potato from the array list. Also it should be case insensitive.
This is the full solution of your requirement as I understood:
String measuring = "\"units\", \"kg\", \"kilogms\", \"kilo\", \"literes\", \"Liter\", \"packets\", \"packet\", \"gms\", \"grams\", \"half kg\"";
String items = "\"Potato\", \"rice\", \"Eggs\", \"Maggi\", \"Dryfruits\", \"Maza\", \"cold drink\", \"sauce\", \"catchup\", \"coconut oil\"";
String matching = "Kg500 Potato";//"Potato 1 kg";
String item = "", measuringUnit = "", quantity = "";
private void findOut() {
String[] sMeasuringArray = measuring.replace("\"", "").split(", ");
ArrayList<String> measuringList = new ArrayList<String>(Arrays.asList(sMeasuringArray));
String[] sItemsArray = items.replace("\"", "").split(", ");
ArrayList<String> itemsList = new ArrayList<String>(Arrays.asList(sItemsArray));
String[] sMatchingArray = matching.split(" ");
matching = matching.toUpperCase();
for (int i = 0; i < measuringList.size(); i++) {
if (matching.contains(measuringList.get(i).toUpperCase())) {
measuringUnit = measuringList.get(i).trim();
break;
}
}
for (int i = 0; i < itemsList.size(); i++) {
if (matching.contains(itemsList.get(i).toUpperCase())) {
item = itemsList.get(i).trim();
break;
}
}
if (matching!= null) {
String[] part = matching.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
for (int k = 0; k < part.length; k++) {
try {
Integer.parseInt(part[k]);
quantity = part[k];
break;
} catch (Exception ex) {
continue;
}
}
}
/*if (sMatchingArray != null) {
if (sMatchingArray.length == 3) {
for (int j = 0; j < sMatchingArray.length; j++) {
if (measuringUnit.trim().equals(sMatchingArray[j].trim())) {
quantity = sMatchingArray[j - 1].trim();
break;
}
}
} else if (sMatchingArray.length == 2) {
String[] part = matching.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
for (int k = 0; k < part.length; k++) {
try {
Integer.parseInt(part[k]);
quantity = part[k];
break;
} catch (Exception ex) {
continue;
}
}
}
}*/
Log.e("Solution: ", "item = " + item + ", measuringUnit = " + measuringUnit + ", quantity = " + quantity);
}
I'm gonna be using algorithmic approach for the answer. So here it goes:
strItem = "1kg rice";
//Run a loop through the list of units and for each unit check this
if (strItem.contains(list.get(index)))
//Do the needful and break
I have this requirement that I need to replace URL in CSS, so far I have this code that display the rules of a css file:
#Override
public void parse(String document) {
log.info("Parsing CSS: " + document);
this.document = document;
InputSource source = new InputSource(new StringReader(this.document));
try {
CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null);
CSSRuleList ruleList = stylesheet.getCssRules();
log.info("Number of rules: " + ruleList.getLength());
// lets examine the stylesheet contents
for (int i = 0; i < ruleList.getLength(); i++)
{
CSSRule rule = ruleList.item(i);
if (rule instanceof CSSStyleRule) {
CSSStyleRule styleRule=(CSSStyleRule)rule;
log.info("selector: " + styleRule.getSelectorText());
CSSStyleDeclaration styleDeclaration = styleRule.getStyle();
//assertEquals(1, styleDeclaration.getLength());
for (int j = 0; j < styleDeclaration.getLength(); j++) {
String property = styleDeclaration.item(j);
log.info("property: " + property);
log.info("value: " + styleDeclaration.getPropertyCSSValue(property).getCssText());
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
However, I am not sure whether how to actually replace the URL since there is not much a documentation about CSS Parser
Here is the modified for loop:
//Only images can be there in CSS.
Pattern URL_PATTERN = Pattern.compile("http://.*?jpg|jpeg|png|gif");
for (int j = 0; j < styleDeclaration.getLength(); j++) {
String property = styleDeclaration.item(j);
String value = styleDeclaration.getPropertyCSSValue(property).getCssText();
Matcher m = URL_PATTERN.matcher(value);
//CSS property can have multiple URL. Hence do it in while loop.
while(m.find()) {
String originalUrl = m.group(0);
//Now you've the original URL here. Change it however ou want.
}
}