I have calendar and I want to pick a proper day, test will be executed everyday and I have method which takes today's date.
I have WebElement pickDay which should find wanted day, the HTML code is:
<td class="class" <data-hanlder="selectDay" data-month="5" data-year="2018">
<a class="classX">5</a>
</td>
and xPath expression is (for example):
String whichMonth = "5"
String whichDay = "5"
day = driver.findElement(By.xpath("//*[contains(#text(),"+whichDay+") AND (#data-month,"+whichMonth+")]"));
But intellij keeps telling me:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(#text(),5) AND (#data-month,5)]' is not a valid XPath expression.
And I dont really know what the problem is with this expression.
1) text() is a function, not an argument. # not needed here
2) don't use contains() for month to avoid mess (1, 10, 11, 12 for example)
Try the following one:
//*[#data-month="+whichMonth+"]/a[text()="+whichDay+"]
Please try following, because it looks like there is missing apostrophes in your xpath :
day = driver.findElement(By.xpath("//*[contains(text(),'"+whichDay+"') AND (#data-month,'"+whichMonth+"')]"));
Related
Not able to iterate through WebElement List
Hi
I am doing some action on https://www.makemytrip.com/ under Departure. When click on Departure, it will show calendar of two months.
I am able to get two webElements with same xpath as mentioned below, now I want to go one by one, and getText() to match with user month name.
List<WebElement> month_list= driver.findElements(By.xpath("//div[#class='DayPicker-Month']"));
ListIterator<WebElement> listIter= month_list.listIterator();
while(listIter.hasNext()) {
WebElement elem= listIter.next();
System.out.println(elem.findElement(By.xpath("//div[#class='DayPicker-Caption']/div")).getText());
}
But every time, it is reporting same text :
July 2019
July 2019
However, I am expecting it should report :
July 2019
August 2019
Please guide me how can I perform anything on each element one by one.
You should try using . instead of / in you second xpath as below :-
System.out.println(elem.findElement(By.xpath("./div[#class='DayPicker-Caption']/div")).getText());
Absolute vs relative XPaths (/ vs .)
/ introduces an absolute location path, starting at the root of the document.
. introduces a relative location path, starting at the context node.
you can make use of foreach loop. Try the below code
for(WebElement monthName: month_list)
System.out.println(monthName.getText())
P.S. I doubt your Xpath ,the xpath which you wrote is the entire month calendar(that is the month name , the dates , the prices on each dates so its the entire div that you have picked)
Kindly try
List<WebElement> month_list= driver.findElements(By.xpath("//div[#class='DayPicker-Caption']"));
You can Try using,
List<WebElement> monthname= driver.findElements(By.xpath("//div[#class='DayPicker-Caption']"));
After Storing in List of Webelements you can use different kind of for Loops for getting the text.But the best preference is forEach Loop and for Iterative Loop.
forEach Loop:
for(WebElement months: monthname)
{
System.out.println(months.getText())
}
General forLoop
int length= monthname.size();
for (int i = 0; i<length; i++)
{
String name = monthname.get(i);
System.out.println(name);
}
PS: But The Best Preference is to go with forEachLoop Rather than General For Loop.
I am using Selenium in java for testing a search wizard where I need to click on a current date. I am trying to traverse through all the dates in the calendar and if matches the current date, I click it.
The problem here is that the dates are hidden so I have to get the innerHTML attribute, then extract the date number using substring and then comparing it with Calendar.getInstance().get(Calendar.DAY_OF_MONTH) but my condition is failing every time. Have tried str.trim() and also tried to remove all the unicode characters but nothing worked. Below is the code:
WebElement day_of_month = AutoUtils.findElementByTagName(day, "span");
if(day_of_month!=null){
String innerHtml = day_of_month.getAttribute("innerHTML");
// innerHtml is "23<span class="buzz-message">Great price</span><i></i>"
// where 23 is the date i need
String exact_date = innerHtml.substring(0,innerHtml.indexOf("<span"));
exact_date = exact_date.replaceAll("\\P{Print}", ""); // have also tried exact_date.trim();
if(exact_date.equals(Calendar.getInstance().get(Calendar.DAY_OF_MONTH))){
day_of_month.click();
} else{
System.out.Println("not found"); //gets printed every time
}
}
Can somebody help?
You have to convert the int value (day of month) to String:
if (exact_date.equals(String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)))) {
...
}
try using innerText instead of innerHTML . innerText only gives you what is visible to the user . innerHTML gives even html and javascripts inside it.
You should be able to get "23" only and do a easy comparison.
Also, if you can show the html , it might be easy for us to help you .
I need to parse a a string which contains the query for the database.
The valid strings can be the following :
Status:OPEN,PENDING
Status:OPEN
Status:PENDING
Status:REJECTED
type:SMALL
type:BIG
weight>100
The following is not valid :
weight:100<PENDING
My first try was this in Java:
Pattern p = Pattern.compile("(\\w+?)(:|<|>)(\\w+)(|,)*(\\w+)*$");
Obviously, it fails to parse the last statement correctly.
I think that validating this with a regexpression is a hrad thing.
But you can try this expression:
"([\\w]+)(:(([A-Z]+)|([A-Z]+,([A-Z]+)))|(>[0-9]+))"
It worked for me in a online regexpression tester for your inputs
I created this expression for your requirement:
Pattern pattern = Pattern.compile("((Status:(OPEN|PENDING|OPEN,PENDING|REJECTED))|(type:(SMALL|BIG))|(weight[<>]\\d+))");
anything else matches invalid status, types, etc.
For example:
These are all invalid:
Status>100
type:REJECTED
weight:100
Hope this helps!
In JMETER I have HTTP query which returns JSON String as following:
{"url":"/some/path?Id=343\u0026"}
I'm trying to parse parameter Id from it with BeanShell sampler:
url = prev.getResponseDataAsString();
int start=url.indexOf('Id=');
int end = url.indexOf('u0026')-1;
newId=url.substring(start,end);
vars.put("newId", newId);
and get an error: Token Parsing Error: Lexical error at line 4, column 25. Encountered: "u" (117), after : "\'s"
Any ideas?
So it seems like backslash confuses the parser. Tried some Java String operations (replaceAll, URLEncoder.encode) - none of them seems to help.
JSON URL contained extra parameter: {"url":"/some/path?Id=343\u0026success=1"}, so following code worked:
url = prev.getResponseDataAsString();
int start=url.indexOf("Id=")+3;
int end = url.indexOf("success=1")-6; //note: "\u0026" is 6 characters
newId=url.substring(start,end);
vars.put("newId", newId);
\u0026 is actually one character, not 6. Change line 3 to
int end = url.indexOf("\u0026");
By the way, I am not sure about your usage of single quotes. I suspect BeanShell is converting from a character array to a String in order to make it work. Best make them double quotes, just in case.
I made a Oracle Package like below.
And I will pass parameter String like '2014-11-05'.
--SEARCH 2014 11 04
FUNCTION SEARCHMYPAGE(v_created_after IN DATE, v_created_before IN DATE)
return CURSORTYPE is rtn_cursor CURSORTYPE;
BEGIN
OPEN
rtn_cursor FOR
select
news_id
from
(
select
news_id,
news_title, news_desc,
created, news_cd
from
news
)
where
1=1
AND (created BETWEEN decode(v_created_after, '', to_date('2000-01-01', 'YYYY-MM-DD'), to_date(v_created_after, 'YYYY-MM-DD'))
AND (decode(v_created_before, '', sysdate, to_date(v_created_before, 'YYYY-MM-DD')) + 0.999999));
return rtn_cursor ;
END SEARCHMYPAGE;
I confirmed my parameter in Eclipse console Message, since I am working on Eclipse IDE.
I got contents, which are made in 2014-10-29 ~ 2014-10-31.
when I pass '2014-11-01' as created_after, It returns 0 records.(But I expected all contents, since every contents are made between 10-29 and 10-31)
Would you find anything wrong with my Function?
Thanks :D
create function search_my_page(p_created_after in date, p_created_before in date)
return cursortype
is rtn_cursor cursortype;
begin
open rtn_cursor for
select news_id
from news
where created between
nvl(v_created_after, date '1234-01-01')
and
nvl(v_created_before, sysdate) + interval '1' day - interval '1' second;
return rtn_cursor;
end search_my_page;
/
Changes:
Re-wrote predicates - there was a misplaced parentheses changing the meaning.
Replaced to_date with date literals and variables. Since you're already using ANSI date format, might as well use literals. And date variables do not need to be cast to dates.
Replace DECODE with simpler NVL.
Removed extra parentheses.
Renamed v_ to p_. It's typical to use p_ to mean "parameter" and v for "(local) variable".
Removed extra inline view. Normally inline views are underused, in this case it doesn't seem to help much.
Removed unnecessary 1=1.
Replaced 0.99999 with date intervals, to make the math clearer.
Changed to lower case (this ain't COBOL), added underscores to function name.
Changed 2000-01-01 to 1234-01-01. If you use a magic value it should look unusual - don't try to hide it.