Not able to select date from date picker using selenium webdriver - java

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);
}

Related

Automate/handle 5th dropdown url https://jedwatson.github.io/react-select/ named as Github users(Aysnc with fetch.js)

I have problem to automate this drop down using selenium web driver using Java
This is the link - Go to 5th drop down named as Github users (fetch. js)
I am not able to enter the data into search field.
I am using send keys after perform click but it throws an exception like this " element is not interact able"
Steps I follow
driver.findElement(By.xpath("xapth")).click
drop down opens with no options because it is searchable and options are coming dynamically after entering key word into the search field.
driver.findElement(By.xpath("xapth")).sendkeys("Test");
Sendkeys are not working in this case because of drop down closed when perform send keys action.
<div class="Select-placeholder">Select...</div>
Below is the code which is working.
Please do optimize the code by removing thread.Sleep and putting some meaningful waits as per your requirement.
driver.Navigate().GoToUrl("https://jedwatson.github.io/react-select/");
IWebElement element1 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']"));
IWebElement element2 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']/div[2]/input[1]")) ;
element1.Click();
Thread.Sleep(2000);
element2.SendKeys("Test");
Thread.Sleep(1000);
element2.SendKeys(Keys.Tab);
Please note that element2 gets activated once you click on element1.
Try the following code:
public void typeAndSelect() {
WebElement searchBox = driver.findElement(By.xpath("//div[#class='section'][5]//div[#class='Select-control']"));
searchBox.click();
WebElement inputField = driver.findElement(By.xpath("//div[#class='section'][5]//input[#role='combobox']"));
inputField.clear();
String searchWord = "test";
inputField.sendKeys(searchWord);
WebElement selectDropdown = driver.findElement(By.xpath("//div[#class='Select-menu-outer']//div[#role='option'][text()='" + searchWord +"']"));
// wait for search results.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(selectDropdown)).click();
}
Correct the following xpath part
"//div[#class='section'][5]"
to your implementation of the dropdown

How to select a date from bootstrap calendar which is read only in selenium webdriver using java?

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;
}

How to Enter Text in Contact form using selenium Webdriver?

public static void main(String[] args)
{
WebDriver wd = new FirefoxDriver();
wd.manage().window().maximize();
wd.get("http://www.arthritisspecialityclinic.com");
WebElement link=wd.findElement(By.linkText("CONTACTS"));
link.click();
WebElement Name = wd.findElement(By.xpath(".//*[#id='contact-form']/fieldset/label[1]/span[3]"));
Name.sendKeys("sakthivel");
}
I've executed the above code for Enter text in NAME text box under contact Form in website..But the text is not typed in the specific field only shown Blank...No error also shown in web driver...Any one can help me to Fix this....
Your xPath is wrong. You should select the input tag instead of the span tag. Try this:
WebElement name = wd.findElement(By.xpath("//form[#id='contact-form']/fieldset/label[1]/input"));
name.sendKeys("sakthivel");
Suggestion: This would be a cleaner method to select the WebElement.
WebElement name = wd.findElement(By.xpath("//input[#name='name']"));
name.sendKeys("sakthivel");

not able to select date from date picker

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();
}

Select DropDown option using selenium

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();

Categories