Selenium - Python: send_keys does not send data - selenium

I have a problem.
I am trying to fill out the Facebook registration form using selenium. I can fill in all the fields except the 'month' field and I don't really understand why. I don't get any error, the field is simply not filled in. I am sure that the values are generated correctly, but I cannot understand why I cannot fill in the month field. This is the part of the code in question:
from faker import Faker
fake = Faker()
day = fake.day_of_month()
month = fake.month()
year = random.randint(1982, 1995)
driver.find_element(By.ID, "day").send_keys(day)
time.sleep(3)
#driver.find_element(By.ID, "month").send_keys(month)
#time.sleep(3)
#m = Select(driver.find_element(By.ID, "month"))
#m.select_by_value(month)
driver.find_element(By.NAME, "birthday_month").send_keys(month)
time.sleep(3)
driver.find_element(By.NAME, "birthday_year").send_keys(year)
time.sleep(3)
Day and year are filled in correctly, as opposed to month. I have tried everything, including not having 'Faker' generate the random values, but nothing. Can you help me?

One reason it's not working could be that fake.month() function returns integer values while Facebook registration form declares string values such as 'May' 'June' etc. I suggest you try with a list of months.

Related

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

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

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']

How to sendkeys "time" in time type element?

I need to pass data in time format in "time" type element in "10:00 AM" format.
I am using following code:
public static void setShift()
{
txttime.sendkeys("1030AM");
}
this is not working. what is a correct way to enter such data?
Use Following Code :
It will work it for textbox/text area control
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a");
Date date = new Date();
txttime.sendkeys(date);
For the HTML input type datetime-local handling it from selenium is not ideal. It is not the most used date time picker, and it is not supported in firefox or safari.
For chrome, the date time format shows the format set in the browser's machine.
If you haven't changed anything, I'm guessing you are getting the format shown in the guru99 tutorial.
If that is the case, then you have missed that they also have provided the solution there.
After entering the date part you need to press tab to input the time part. Which is missing from your given code.
Try this:
First, input the date
WebElement dateBox = driver.findElement(By.xpath("//form//input[#name='bdaytime']"));
dateBox.sendKeys("09252013");
Second, press tab
dateBox.sendKeys(Keys.TAB);
Last, input the time
dateBox.sendKeys("0245PM");
Note:
If your machine has different DateTime formatting then this might not work. You have to check which part of the date time senKeys can actually input then split up that part and use Keys.TAB to press tab. Then input the next part until completion.

How to deal with Date Picker

I've to select a user specified date and the snippet for picker is below.
sendkeys() functionality isn't working so I tried the below code.
JavascriptExecutor check = (JavascriptExecutor)driver;
check.executeScript("document.getElementById('hotel-checkin').setAttribute('value','10 Jan 2018')");
On entering the value, the date picker stays alive wherein the script fails to click the search button which is actually overlapped by date picker.
Any leads would be of great help. Thanks in advance!
Ok, you asked for leads so here're some thoughts and wild guesses (as long as you didn't give us neither url nor html).
sendkeys() doesn't work because the input field most probably has an event handler attached that opens datepicker widget. So, your first try to set value through JS is OK. I use slightly different way (sorry, python code here): driver.execute_script("arguments[0].value = arguments[1];", webelement, value)
But this mightn't work because you're entering a value to the visible field and an actual field (supposed to be filled by the datepicker widget) used by the page (a form or JS code) is hidden and holds no value. Then, try to find that field and enter a value to it by JS method.
In my current project I couldn't find that hidden field. I've decided to implement full user-like interaction with the datepicker widget. The scenario: click on the date field (datepicker widget opens), click on year or month selector buttons, then click on a day wanted. This way widget sets relevant data in a relevant fields (whatever and wherever they are) and closes.
Why I said fields (plural)? My recent thought was: May be setting the visible date field is right but there's some other field that must be set (like, say, date_was_set = "true").
Hope, this was helpful.
Edit. As for the 3rd paragraph. Here is my somewhat edited example (python, again).
The datepicker looks like this
As you can see it has buttons to adjust month and year combined.
from selenium.webdriver.support import expected_conditions as EC
class SetValCalendarStrategy(object):
def __init__(self, driver, calendar_param):
self.driver = driver
# this object holds parameters to find the calendar itself and its components
# pairs:
# sel_type - selector type ('xpath', 'id', 'name' etc.)
# sel_value - selector value to find ("//tr/td" etc)
self.param = calendar_param
def __call__(self, field, value, timeout):
"""
:param <webelement> field - input field
:param <datetime> value - value (date) to set
"""
# initiate datepicker with click on date input field
field.click()
# wait for a widget to show
cal = WebDriverWait(self.driver, timeout, 0.3).until(
EC.visibility_of_element_located(
(self.param.sel_type, self.param.sel_value)))
# decrease month/year button
prev_button = cal.find_element(
self.param.prev_month.sel_type,
self.param.prev_month.sel_value)
# increase button
next_button = cal.find_element(
self.param.next_month.sel_type,
self.param.next_month.sel_value)
today = datetime.now()
# calculate difference in months between today and the target date
month_diff = value.month + (value.year - today.year) * 12 - today.month
# select month/date
if month_diff < 0:
button = prev_button
else:
button = next_button
for i in range(abs(month_diff)):
button.click()
# template looks like this. It selects only days from target month (bold font)
# "//div[contains(#class, 'datePickerDay') and not(contains(#class, 'datePickerDayIsFiller')) and text()='{}']"
# insert day (21) into template. then it becomes
# "//div[contains(#class, 'datePickerDay') and not(contains(#class, 'datePickerDayIsFiller')) and text()='21']"
day_picker_sel_value =
self.param.day_picker.sel_template.format(value.day)
day = cal.find_element(
self.param.day_picker.sel_type,
day_picker_sel_value)
day.click()

Issue with the payment flow using selenium webdriver

I've a scenario where I need to automate the payment page.
I need to enter the credit card details, but the thing is when I use sendkeys method to type the text, it doesn't take the full card number though it only takes the first 4 chars.
The card field is designed in such a way that it takes 4 numbers and then provide auto space and then next 4 numbers and so on...
How would I handle this particular thing?
You can use this as this code sends the character one by one and I hope that the payment gateway will accept the cardnumber if the characters are sent one by one.
for(int i=0;i<ccNumber.length();i++){
creditcardtextbox.sendKeys(ccNumber.substring(i).split(""));
}
I'm having the same issue, using Python and Selenium and the credit card field only takes 4 characters
Here is the code
#move to the Shipping screen
elem.send_keys(Keys.RETURN)
#move to the Payment, no return key here, press the button
driver.implicitly_wait(1) # seconds
driver.find_element_by_name("button").click()
#enter the payment details
driver.implicitly_wait(1) # seconds
elem = driver.find_element_by_id("checkout_credit_card_number")
elem.send_keys(str(sys.argv[5]), Keys.ARROW_DOWN)
elem = driver.find_element_by_id("checkout_credit_card_name")
elem.send_keys(str(sys.argv[6]))
elem = driver.find_element_by_id("checkout_credit_card_month")
elem.send_keys(str(sys.argv[7]))
elem = driver.find_element_by_id("checkout_credit_card_year")
elem.send_keys(str(sys.argv[8]))
elem = driver.find_element_by_id("checkout_credit_card_verification_value")
elem.send_keys(str(sys.argv[9]), Keys.ARROW_DOWN)
You can do this by :
driver.find_element_by_id("checkout_credit_card_number").sendKeys("5123");
driver.find_element_by_id("checkout_credit_card_number").sendKeys("4567");
driver.find_element_by_id("checkout_credit_card_number").sendKeys("8901");
driver.find_element_by_id("checkout_credit_card_number").sendKeys("2346");
Here's my solution mocked in Python:
from selenium.webdriver.common.by import By
ccn = '4111111111111111'
ccn_locator = driver.find_element(By.ID, 'numberField')
for num in ccn.replace('', ' ').strip().split():
ccn_locator.send_keys(num)
You can do like below to handle space between every 4 digits :
driver.findElement(By.id("elementid")).sendKeys("1");
driver.findElement(By.id("elementid")).sendKeys("2");
driver.findElement(By.id("elementid")).sendKeys("3");
driver.findElement(By.id("elementid")).sendKeys("5");
driver.findElement(By.id("elementid")).sendKeys(Keys.SPACE);
driver.findElement(By.id("elementid")).sendKeys("5");
driver.findElement(By.id("elementid")).sendKeys("1");
driver.findElement(By.id("elementid")).sendKeys("2");
driver.findElement(By.id("elementid")).sendKeys("3");
driver.findElement(By.id("elementid")).sendKeys(Keys.SPACE);
Above is sample code , please replace your actual element ID and digits. Also I have given just sample for 8 digits , you can do same for 16 digits. Just call that SPACE key after every 4 digits. Your elementid will be same for all lines.
Note : There may be other way to do above but I have tried above and working fine so suggesting you same.