Selenium select date from a calendar- unable to find x path - selenium

I am trying to select a date from the calendar. However sendkeys does not send data. After I click on the field calendar month drops down. Right click reveals nothing. I have tried in Firefox and Chrome.
http://demo.guru99.com/V4/manager/addcustomerpage.php
username: mngr284483
password: AjerYbu
#FindBy(xpath = "//input[#name='dob']")
private WebElement selectCustomerDOB;
public void sendDOB(String dob){
selectCustomerDOB.sendKeys(dob);
}
I have tested with Thread.sleep to make sure it is not a timing issue.
What can I do in this case to select a date? Thanks in advance for your time and ideas.

Since you are using an element with type 'date', it is up to the browser to render the calendar, or anything else to allow users to select the date. The date picker is not part of your web page and so you would not be able to find its locators in the conventional ways.
I believe your testing requirement is to be able to check how your application behaves when different dates are selected, rather than to see if the date picker works (which the browser would take care for you).
In order to select dates, you should be able to use send keys if you gave the value in the right format (yyyy-MM-dd). I tested with the given website using Selenium IDE and things seem to work.
WebElement selectCustomerDOB = driver.findElementById("dob"); // While xPath is great and your xPath is valid - I feel ID is much faster.
selectCustomerDob.sendKeys("2011-12-21");

Related

Selenium Google Trends Custom period

I am trying to get daily data from Google trends using Selenium.
Therefore, I need to Input the start and end date in both boxes. For the start date, I use the following which works fine:
elem4 = browser.find_element_by_class_name("md-datepicker-input")
elem4.clear()
elem4.send_keys("01/01/2018")
However, the second box has the same class name 'md-datepicker-input'.
Picking it via xpath also doesnt work...
Has anyone an idea how to select that box, clear it and input my data?
Example custom period
Here is the CSS that you should use for To date.
.custom-date-picker-dialog-range-to .md-datepicker-inpt
If you want to use xpath
//div[#class='custom-date-picker-dialog-range-to']//input[#class='md-datepicker-input']

Unable to put date in <input type="date"> field in Firefox using Selenium

I am sending date of birth to an input element using selenium, data is getting entered in Chrome and Edge but in firefox that input element has an inbuilt datepicker and I am not able to inspect that default datepicker (right click doesn't work on datePicker and couldn't find any code for it). The input element is not taking values and I can't get the html element so I'm unable to fill the field.
Can anyone help me with this?
For Firefox, the date format is yyyy-mm-dd , regardless placeholder. I read Flaburgan's comment in https://github.com/mozilla/geckodriver/issues/1070 and searched documents, tested okay.
WebElement dateOfBirth = driver.findElement(By.name("dob"));
dateOfBirth.sendKeys("2012-12-24");
You handle this issue with the following trick:
Change the input type from date to text using JavaScriptExecutor by Selenium
After changing the element type to text the change the date field by sending text with the same format needed to change value, use sendKeys("dd/mm/yyyy").
OR try to use Selenium Actions move to element and click.

Protractor sendKeys issue with scripted input fields

I'm automating e2e tests with Protractor on an angular app.
However, I have an issue when sending keys on input fields.
The sendKeys would miss few characters everytime so I found a workaround :
static sendKeys(value, element){
value.split('').forEach((c) => element.sendKeys(c));
}
This works well but it takes more than 3 times the time the original sendKeys function would.
Well no problem my tests are still functionnal right ?
My app now has new fields with scripts behind them.
One of them is a datepicker input, you can either choose from the datePicker or type it manually. However, for today's date you would type 09022018 and the slashes are automatically appended at the right place (like so 09/02/2018). If you were to enter a wrong date the field is cleared.
Now back to the problem : it seems that both my implementation of sendKeys and the original one loose focus after each submitted key. This means that I can't enter a valid date in the input field as it's cleared after each simulated keypress.
I could use browser.executeScript to fix it but I wouldn't be able to test the functionnality adding slashes. Also, as you type, the datepicker is still open and refreshes after each keypress, you can select a date from it at any time and that is also a feature I want to test.
Thanks in advance
Use executeScript to set the date in backgrond, then use sendKeys to enter a space or Tab at the end to trigger the Keyborad event which will check the input and format the input with slash
function enterDate(date) {
var script = 'arguments[0].value=arguments[1]';
// input box for date
var dateBox = element(by.xxx(yyy));
browser.executeScript(script, dateBox, date);
dateBox.sendKeys(" ");
// or try send Tab
dateBox.sendKeys(protractor.Key.TAB);
}
enterDate('09022018');
You can try this solution on other fields you fixed but take 3 more time.

Capybara, selecting 1st option from dropdown?

I've done a search and most of the related google results have returned just in general selecting an element from a dropdown. However the ID's in this case for the elements in the dropdown are dynamically generated unfortunately.
This is for a base test case, so I basically just need to select for example the first one. The text is also the same for the elements in the dropdown (not sure if that helps).
Is there such an example of this?
Im using cucumber with caybara(using selenium driver) integration
You can find the first option element and then use the select_option method to select it.
For example, if the select list has an id "select_id", you can do:
first('#select_id option').select_option
As #TomWalpole mentions, this will not wait for the element to appear. It would be safer to do one of the following:
first('#select_id option', minimum: 1).select_option
or
find('#select_id option:first-of-type').select_option
Alternatively you can get the first element text then select it by select function:
first_element = find("#id_of_dropdown > option:nth-child(1)").text
select(first_element, :from => "id_of_dropdown")
After two days of searching and reading, this article was amongst one of a few that was helpful. Hopefully, this can help someone else!
I created a few methods like so, excuse the naming..I changed it.
def some_dropdown(id, text)
dropdown = find(id).click
dropdown.first('option', text: text).select_option
end
def select_form
within 'content#id' do
some_dropdown('#id', text)
click_link_or_button 'Submit'
end
end
I also referenced this.
I've tried to select an option from a modal dropdown. After trying all listed methods, and many other from other threads - I totally gave up and instead of using clicks or select_option just used keyboard keys
find(:select, "funding").send_keys :enter, :down, :enter
In case it still complains - try:
find(:select, "funding", visible: false).send_keys :enter, :down, :enter
Worked like a charm, selecting first option from a dropdown.

Selenium SendKeys not working for open brackets and harsh keys when using java

I have using selenium webdriver 2.33.0 and i have a requirement of sending data with the following characters inside the data ("(", "#")
When we tried to send these characters using sendkeys
"WebElement dat = driver.findElement(By.xpath("xpathexpression);
dat.sendkeys("select * from (?s, ?p, ?o)");
The following data gets displayed in the textarea as select * from ?s, ?p, ?o)
the open bracket is missing. I have used selenium actions class sendkeys and robot sendkeys also.
I am not able to fix the issue. Can someone help me on this?
Ugly but efficient workaround: replace the opening brackets ( with the key sequence shift + 9, as suggested by user2935099.
dat.sendKeys(Keys.chord(Keys.SHIFT, "9"));
Funnily, this seems to work regardless of your current keyboard layout (I use an Irish locale with a French layout).
I stumbled upon this one with version 2.40.0, and it is definitely a bug in Selenium. Using the following in the Firefox JavaScript console works flawlessly:
var box = getElementById('SearchBox');
box.setValue('AB (CDE FGH)');
The issue is resolved when using actions class
WebElement dat = driver.findElement(By.xpath("xpathexpression");
dat.click();
Actions data = new Actions(driver);
data.sendKeys(Keys.chord(Keys.CONTROL,"a"),Keys.DELETE);
data.perform();
query = query.replaceAll("\\(", Keys.chord(Keys.SHIFT,"9"));
query = query.replaceAll("\\#", Keys.chord(Keys.SHIFT,"3"));
query = query.replaceAll("\\-", Keys.SUBTRACT.toString());
query = query.replaceAll("\\}", Keys.chord(Keys.SHIFT,"]"));
data.sendKeys(query);
data.perform();
I tried entering the same thing with my selenium setup and it worked fine in both firefox and chrome. I am not not very sure about the reason but one thing that I can notice that you are using very old selenium version(2.33.0) currently 2.37.0 is available. May be you should try updating the selenium and t should work as expected.
Same problem here, using 2.40.0 my workaorund was using clipboard, not pretty but testsystems have not much use for a clipboard anyway...
public static void setTextByClipboard(WebElement element, String text) {
StringSelection selection = new StringSelection(text);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
element.sendKeys(Keys.chord(Keys.CONTROL, "v"));
}