This is the website I am testing https://www.sydneyairport.com.au/go/car-parking.aspx , I am almost done , but have been stuck in a single issue .
I have selected "date and time" from entry-date section but cannot select "date and time" from exit-date section.
I am clueless why I am not able to it since its the same structure for both and I was able to do it for entry-date ,I don't understand what changes for exit date section . I am new to selenium and will appreciate if anybody helps me out .
This is what I have written to select date and time in entry date section .
public void selectDate(WebDriver driver, String fromDate, String toDate) {
// selects from date
WebElement dateButton = driver.findElement(By.id("period_picker_0"));
dateButton.click();
WebElement datepicker = driver.findElement(By.xpath("//div[#class='period_picker_days']/table/tbody/tr/td[1]"));
selectDate(datepicker, fromDate);
WebElement timeBox = driver.findElement(By.xpath("//div[#class='period_picker_work']/div[2]/input"));
timeBox.sendKeys("");
WebElement time = driver
.findElement(By.xpath(".//*[#id='timepicker_box_start']/div/div[2]/div/div[1]/div[13]"));
time.click();
// Selects to date
WebElement dateButton2 = driver.findElement(By.id("period_picker_1"));
dateButton2.click();
// dateButton.click();
WebElement datepicker2 = driver
.findElement(By.xpath("//div[#class='period_picker_days']/table/tbody/tr/td[2]"));
selectDate(datepicker2, toDate);
WebElement timeBoxEnd = driver.findElement(By.xpath("//div[#class='period_picker_work']/div[2]/input"));
timeBoxEnd.sendKeys("");
WebElement timeEnd = driver
.findElement(By.xpath(".//*[#id='timepicker_box_end']/div/div[2]/div/div[1]/div[13]"));
timeEnd.click();
}
public int selectDate(WebElement datepicker, String date) {
int ele = 0;
List<WebElement> rows_table = datepicker.findElements(By.tagName("tr"));
int rows_count = rows_table.size();
for (int row = 0; row < rows_count; row++) {
// To locate columns(cells) of that specific row.
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
// To calculate no of columns(cells) In that specific row.
int columns_count = Columns_row.size();
// Loop will execute till the last cell of that specific row.
for (int column = 0; column < columns_count; column++) {
// To retrieve text from that specific cell.
if (Columns_row.get(column).getText().equals(date)) {
ele = column;
Columns_row.get(column).click();
}
}
}
return ele;
}
I did lot of work for this code, You are using tr and td. I would suggest there is no need of it.
See the code I'm using, i am able to select both date easily. Hope this help you..
driver.get("https://www.sydneyairport.com.au/go/car-parking.aspx");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='period_picker_0']")).click();
Actions a = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver,20);
WebElement entrydate= driver.findElement(By.xpath(".//*[#id='body']/div[1]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[5]/td[5]"));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='body']/div[1]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[5]/td[5]")));
a.moveToElement(entrydate).build().perform();
Thread.sleep(5000L);
entrydate.click();
WebElement entrytime= driver.findElement(By.xpath(".//*[#id='timepicker_box_start']/div/div[2]/div/div[1]/div[15]"));
a.moveToElement(entrytime).build().perform();
Thread.sleep(5000L);
entrytime.click();
WebElement exitdate= driver.findElement(By.xpath(".//*[#id='body']/div[2]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[6]/td[5]"));
a.moveToElement(exitdate).build().perform();
Thread.sleep(5000L);
exitdate.click();
WebElement exittime= driver.findElement(By.xpath(".//*[#id='timepicker_box_end']/div/div[2]/div/div[1]/div[15]"));
a.moveToElement(exittime).build().perform();
Thread.sleep(5000L);
exittime.click();
Please do reply, how you find this and reply back to me for further query.
Happy Learning. :-)
Related
List<WebElement> tdata=driver.findElements(By.tagName("tr"));
List<WebElement> tdata1=driver1.findElements(By.tagName("tr"));
for(int i=0,j=0; i<tdata.size() && j<tdata1.size();i++,j++ )
{
WebElement row = tdata.get(i);
WebElement row1 = tdata.get(j);
System.out.print(row1.getText());
System.out.print(row1);
if(row.getText().equals(row1.getText()))
{
System.out.println(row.getText());
}
else if(!(row.getText().equals(row1.getText())))
{
System.out.print("Not matching text");
System.out.println(row1.getText());
}
}
This is my code for comparing 2 web table, I am Unable to verify content equal or not equal. For unmatched text also it is not printing anything. else if part is not capturing if elements are not equal.
try
List<WebElement> tdata= ...
List<WebElement> tdata1= ...
int common= 0;
for (WebElement element: tdata)
if (tdata1.contains(element))
common++;
I have a webtable for which I have to fetch the last row . The problem is that the webtable is dynamic so I don't know the row number.I have tried below code but the row number is static there which will fail.
List<WebElement> rows=driver.findElements(By.xpath("//table[1]/tbody/tr[5]"));
to get last element use (locator)[last()] method, like:
WebElement lastRow = driver.findElement(By.xpath("(//table[1]/tbody/tr)[last()]"));
which is shorted variant of:
(locator)[position() = last()]
public class LastRowFetching {
public static void main(String[] args) throws ParseException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver","G://chromedriver.exe");
driver= new ChromeDriver();
driver.get("http://money.rediff.com/gainers/bsc/dailygroupa?");
//No.of rows
List rows = driver.findElements(By.xpath(".//*[#id='leftcontainer']/table/tbody/tr/td[1]"));
System.out.println("No of rows are : " + rows.size());
int lastRow = rows.size();
WebElement lastRowFetch =driver.findElement(By.xpath(".//*[#id='leftcontainer']/table/tbody/tr/td["+lastRowcount+"])"));
driver.close();
}
}
int lastRowcount=driver.findElement(By.xpath("//table[1]/tbody/")).findElements(By.tagName("tr")).size();
WebElement Lastrow =driver.findElement(By.xpath("//table[1]/tbody/tr["+lastRowcount+"]"));
In first Line we get the Last Row count from the table.
On second line we get the Last row Element from the table.
I'm trying to choose elements in "Category" one-by-one on this page with:
WebElement category = new WebDriverWait(driver, 10)
.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("tr[__gwt_row='" + rowIndex + "'] > td > div")))
.get(3);
category.click();
category.findElement(By.tagName("select")).sendKeys("Businesses");
category.click();
category.findElement(By.tagName("select")).sendKeys("Contacts");
Throwing this : "stale element reference: element is not attached to the page document"
Maybe exists some another way to do it?
UPD:
It works if i do this:
WebElement categoryBusinesses = new WebDriverWait(driver, 10)
.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("tr[__gwt_row='" + rowIndex + "'] > td > div")))
.get(3);
categoryBusinesses.findElement(By.tagName("select")).sendKeys("Businesses");
WebElement categoryContacts = new WebDriverWait(driver, 10)
.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("tr[__gwt_row='" + rowIndex + "'] > td > div")))
.get(3);
categoryContacts.findElement(By.tagName("select")).sendKeys("Contacts");
But is it a right way?
UPD2:
I also tried this:
WebElement element = categoryBusinesses.findElement(By.tagName("select"));
Select category = new Select(element);
category.selectByIndex(0);
category.selectByIndex(1);
But it not works.
This was really annoying... the problem is that every time you change the value of the SELECT, the page is recreating the element thus causing your reference to become stale. You have to refetch it basically every time you interact with it.
I generally make functions out of things like this so that they are reusable.
public static void GetCategoriesFromRow(int row)
{
By locator = By.cssSelector("table[__gwtcellbasedwidgetimpldispatchingfocus] tr[__gwt_row='" + row + "'] select");
for (int i = 0; i < new Select(driver.findElement(locator)).getOptions().size(); i++)
{
new Select(driver.findElement(locator)).selectByIndex(i);
System.out.println(new Select(driver.findElement(locator)).getFirstSelectedOption().getText());
}
}
You call it like
driver.get("http://samples.gwtproject.org/samples/Showcase/Showcase.html#!CwCellTable");
new WebDriverWait(driver, 10)
.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table[__gwtcellbasedwidgetimpldispatchingfocus]")));
GetCategoriesFromRow(0);
Try this:
WebElement element = categoryBusinesses.findElement(By.xpath("//*[#id="gwt-debug-contentPanel"]/div[2]/div/div[2]/div/div[3]/div/div/div/table/tbody/tr[1]/td/table/tbody[1]/tr[1]/td[4]/div/select"));
Select category = new Select(element);
category.selectByIndex(0);
category.selectByIndex(1);
This should work.
driver.get("https://www.google.co.in/webhp?
hl=en#hl=en&tbm=nws&q=site+:+www.google.com");
java.util.List<WebElement> dates = driver.findElements(
By.xpath("//div[contains(#class, 'f nsa _uQb')]"));
System.out.println(dates.size());
Trying above code... I am getting output zero, rather than what it should be, 10.
Please suggest if i am doing something wrong...
Hi please find the answer
public class FetchDate {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver .get("https://www.google.co.in/webhp?hl=en#hl=en&tbm=nws&q=site+:+www.google.com");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
List<WebElement> dates = driver.findElements(By.cssSelector(".f.nsa._uQb"));
// for finding size
System.out.println(dates.size());
// for printing every date
for(WebElement Dates : dates){
System.out.println("date for the search is : " + Dates.getText());
}
UPDATE :
// for printing the way you want do it like below
List<WebElement> dates = driver.findElements(By.cssSelector(".f.nsa._uQb"));
for (int j = 0; j < dates.size(); j++) {
String date = dates.get(j).getText();
System.out.println(date);
}
}
}
and the output is :
10
date for the search is : 29-Apr-2016
date for the search is : 03-May-2016
date for the search is : 29-Apr-2016
date for the search is : 20-Apr-2016
date for the search is : 16-Apr-2016
date for the search is : 30-Apr-2016
date for the search is : 25-Apr-2016
date for the search is : 25-Apr-2016
date for the search is : 21-Apr-2016
date for the search is : 07-Apr-2016
Your way of construction xpath is wrong try this one,
//*[contains(concat(' ', #class, ' '), ' f nsa _uQb ')]
java.util.List dates = driver.findElements(
By.xpath("//*[contains(concat(' ', #class, ' '), ' f nsa _uQb ')]"));
Note the spaces
The dates are in <span> tag, not <div>. Try
List<WebElement> dates = driver.findElements(By.xpath("//span[contains(#class, 'f nsa _uQb')]"));
You can also find the dates by the nsa or _uQb classes
List<WebElement> dates = driver.findElements(By.className("nsa"));
List<WebElement> dates = driver.findElements(By.className("_uQb"));
How can we select the dropdown value in selenium webdriver using Testng?
Selecting dropdown values in selenium webdriver is not a part of TestNG, it's part of selenium+java code.
Use below code for reference ::
public class temp {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.shoppersstop.com/shoes/kids-shoes/all/brand/kittens.html");
WebElement selectElement = driver.findElement(By
.xpath("//select[#class='subCatThree' and #name='category']"));
Select select = new Select(selectElement);
List<WebElement> options = select.getOptions();
for (WebElement option : options) {
System.out.println(option.getText());
if (option.getText().equals("Girls ( 3 Years & Above)")) {
option.click();
break;
}
}
}
If you want to select by value
Select select = new Select(driver.findelement(By.xpath("write the xpath of dropdown")));
select.selectByValue("write value here");
If you want to select by Text
Select select = new Select(driver.findelement(By.xpath("write the xpath of dropdown")));
select.selectByVisibleText("write text here");
How do we convert this code to use Selenium WebDriver
If Browser("UOB").Page("pgeSetCustomLimits").Exist(intSyncTime*1) Then
Set oDesc=Description.Create
oDesc("micclass").Value = "WebElement"
oDesc("html id").Value = "limitsInput_CI_form_label_div"
Set ObjEle =
Browser("UOB").Page("pgeSetCustomLimits").ChildObjects(oDesc)
For i=0 to ObjEle.count-1
strWebEleText = ObjEle(i).getRoProperty("innertext")
'print strWebEleText
If Instr(strWebEleText,strPaymentType) Then
intRow = i
Exit For
End If
Next
End If