I have recorded selenium code for selecting date from date picker. While running the test case, the date picker pops up and highlights the selected date correctly. But the date is not selected.
The code looks like:-
driver.findElement(By.id("imgStartDate")).click();
driver.findElement(By.xpath("//td[5]")).click();
driver.findElement(By.xpath("//td[5]")).click();
driver.findElement(By.xpath("//td[5]")).click();
driver.findElement(By.xpath("//td[5]")).click();
// ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]
This exception appears in the recorded code only.I am using selenium-server-standalone-2.45.0 jar.
Recording will not work for date picker... try this code.
try{
WebElement dateWidget = driver.findElement(By.xpath(OR.getProperty(object)));
List<WebElement> rows = dateWidget.findElements(By.tagName("tr"));
List<WebElement> columns = dateWidget.findElements(By.tagName("td"));
for (WebElement cell: columns){
if (cell.getText().equals(data)){
cell.findElement(By.linkText(data)).click();
break;
}
}
}catch(Exception e){
return Constants.KEYWORD_FAIL+" -- Not able to select the date"+e.getMessage();
}
Related
This is the calendar which we are using in the application, this field is read only i have to select the date automatically, i cant be able to inspect the calendar as the code is disappearing automatically when we click on the calendar because of that i cant be able to write code in selenium
DatePicker are not Select element.
Datepicker are in fact table with set of rows and columns.To select a date you just have to navigate to the cell where our desired date is present.
So your code should be like this:
WebElement we = driver.findElement(your locator);
List<WebElement> columns=we.findElements(By.tagName("td"));
for (WebElement cell: columns){
//Select 13th Date
if (cell.getText().equals("13")){
cell.findElement(By.linkText("13")).click();
break;
}
Try the below code,and let me know if it has worked for you
WebElement dateWidget = driver.findElement(By.xpath("Your locator"));
List<WebElement> columns=dateWidget.findElements(By.tagName("td"));
for (WebElement cell: columns){
//Select 13th Date
if (cell.getText().equals("13")){
cell.findElement(By.linkText("13")).click();
break;
}
I am not able to select data from the calendar because it is not having the id's to select.
below is my HTML code,
The Code I tried is
DateDOB = By.xpath("//*[#id='dob']")
strDateDOB="12/12/2020"
driver.findElement(DateDOB).sendKeys(strDateDOB);
but unable to select the date
Url to the application :
http://demo.guru99.com/V4/
username : mgr123
Password : mgr!23
Click on the New Account from the Left Pane to select the above discussed desired page.
Can any one please help in selecting the date?
DOB has the id. sendKeys works. Don't use XPATH when ID is present.
I tried the following code and it is entering DOB value:
#Test
public void tst1() {
WebDriver driver = new FirefoxDriver();
driver.get("http://demo.guru99.com/V4/");
driver.findElement(By.name("uid")).sendKeys("mgr123");
driver.findElement(By.name("password")).sendKeys("mgr!23");
driver.findElement(By.name("btnLogin")).click();
driver.findElement(By.linkText("New Customer")).click();
driver.findElement(By.id("dob")).sendKeys("12/12/2018");
sleep(5000);
}
Driver.findElement(By.xpath("//*[#id='client']")).sendKeys("Ho");
Driver.manage().timeouts().implicitlyWait(1,TimeUnit.MINUTES);
WebElement dropdown = (new WebDriverWait(Driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='client']")));
Driver.findElement(By.xpath("//*[#id='collapseClientInfo']/div/form/div[3]/div[2]/ul/li[1]/a")).sendKeys(Keys.ENTER);
Could you please help me to select auto populate value from drop down list:
We've Client textbox which is an auto-populate box.
When I enter "ho" text in the client field, it shows me the drop down which has values related to my entered text i.e. ho, then I have to select those values which are available under list.
In above code I've tried with Press Enter but unable to select the value.
Could you please check the above code and help me out for the same?
You should try as below :-
WebDriverWait wait = new WebDriverWait(Driver, 60);
//wait until loader invisible
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loaderDiv")));
//this sleep is required because after invisibility of loader focus goes to first input which is Requisition Number
//If you are filling form from first input no need to for this sleep
//if you want to input directly to client field need to sleep to avoid focus first
Thread.sleep(3000);
//Now find the client input and set value
WebElement client = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("client")));
client.sendKeys("Ho");
//Now find all the showing option
List<WebElement> dropdownOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("ul.dropdown-menu a")));
//Now select the first option
dropdownOptions.get(0).click();
Below approach might be helpful:
// Enter text in auto complete text box
driver.findElement(By.xpath("//*[#id='client']")).sendKeys("Ho");
// Wait for options to display
Thread.sleep(5000);
// Option to select
String optionToSelect = "Honda";
Boolean isOptionSelected = Boolean.FALSE;
// Get the options displayed
List<WebElement> options = driver.findElements(By
.cssSelector("ul.dropdown-menu a"));
// Select option
for (WebElement webElement : options) {
if (webElement.getText().equalsIgnoreCase(optionToSelect)) {
webElement.click();
isOptionSelected = Boolean.TRUE;
}
}
if (isOptionSelected) {
// Option is selected
} else {
// Expected option is not displayed. Fail the script
}
Try this:
Select drpCountry = new Select(driver.findElement(By.id("searchOptions")));
drpCountry.selectByVisibleText("By author");
I have a situation like I am automating a native app using Appium(1.3.7) and Selenium webdriver (2.44) in that I want to enter the Date of birth field, when I am clicking on Date of birth field, it will pop up a window to enter the date of birth details, that window is not in focus and appium is failing to enter the details on the window as it is not able to recognize here I am not able to attach the screenshot.
Please help me how to get in to focus and enter the Date of birth details..
Using Google I tried a lot of methods like:
Method : 1
List<WebElement> Listed= dr.findElementsByClassName("android.widget.EditText");
for(int i=0; i <=Listed.size()-1;i++){
System.out.println("EditText Number="+i);
Listed.get(i).sendKeys("10");
Method : 2
WebElement Touch1= dr.findElement(By.xpath("//*[#class='android.widget.EditText']"));
//
TouchAction action = new TouchAction(dr);
action.press(168,440);
action.waitAction(300);
action.perform();
Method : 3
dr.tap(168,440,405,591);
Method : 4
WebElement DOBsample = dr.findElementByXPath("//*[text()[contains(.,'20')]]");
DOBsample.click();
Method : 5
dr.sendKeyEvent(66);
For pickers, you must use swiping over them. I´m using Scala, but logic is obvious:
def setYear(year: Int): Unit = {
require(year >= 1915 || year <= 2015)
val picker = drvr.findElementById("cz.app.yearPicker")
while (picker.getText != year.toString) {
if (picker.getText.toInt > year) {
swipePickerDown(picker)
} else { swipePickerUp(picker) }
}
}
def swipePickerUp(picker: AndroidElement): Unit = {
picker.swipe(SwipeElementDirection.UP, 400)
}
def swipePickerDown(picker: AndroidElement): Unit = {
picker.swipe(SwipeElementDirection.DOWN, 400)
}
If someone could help me with this mystery.. Here is the url
Here if you can see the Date of Birth Fields I am able to select the Month and Date but I am not able to select Year
I Tried selecting Using Value, Index it did not work the same code worked for Month and Date
Below is My Code:
WebElement W = driver.findElement(By.xpath("//html/body/form[#id='aspnetForm'][contains(#action,'ActivateAccount.aspx?key=')][#method='post'][#name='aspnetForm']/div[#class='border4']/div[#id='page']/div[#class='IE-SCroll-mid']/div/div[#class='change-info-contain']/div/div/div/label[3]/select"));
Select dropdown = new Select(W);
dropdown.selectByValue("1997");
Try implementing explicit wait with that. That will make sure the elemenet is present before start looking for that.
By byId = By.id("ctl00_mcp_ddlYear");
//use explicit wait to make sure the element is there
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(byId));
Select dropdown = new Select(myDynamicElement);
dropdown.selectByValue("1997");
You can also use the cssSelector directly wait for the specific option to be present with the following code
//explicit wait
By byCss = By.cssSelector("#ctl00_mcp_ddlYear>option[value='1997']");
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(byCss));
element.click();