Selelnium - Python | Date Of Birth Fields By Using Label
Solution 1:
I don't think it is a good idea to spend too much time on a generic solution when a page specific solution is easy when it is done well. If you are sure that all the pages you want to use use the DD/MM/YYYY format and all the fields are numeric then you may try something like this:
deffill_date(input_holder_div, data):
fields = input_holder_div.find_elements_by_xpath('./input[@type="text"]')
for i inrange(3):
fields[-i-1].clear()
fields[-i-1].send_keys(data[-i-1])
my_date = [11, 11, 1980]
# fill_date(driver.find_element_by_xpath('//*[@f-class="EntryRow"]'), my_date)# fill_date(driver.find_element_by_xpath('//*[@class="rhsForm"]'), my_date)
Calling the function with the proper parent and the data and the job is done. The -i-1
part is required to fill the last 3 input fields only and ignore the hidden input field if such element is present. But there are side effects.
What if the page does not contain a parent element which contains only these 3 input text fields. What if you encounter a page where the date format is YYYY/MM/DD or the months are in a select by name...
If you want to do very similar or the same workflow on two different layouts, then you probably should check out the page object approach. The concept is that you define in a class how to do things on a page and then just call the methods and they do what they meant to do.
Example:
classBasePage:
def__init__(self, driver):
self.driver = driver
@staticmethoddef_fill(input_element, data):
input_element.clear()
input_element.send_keys(data)
classFirstPage(BasePage):
deffill_day(self, data):
field = self.driver.find_element_by_xpath('//*[@class="split-date-day"]')
self._fill(field, data)
classSecondPage(BasePage):
deffill_day(self, data):
field = self.driver.find_element_by_xpath('//*[@id="Entry.Customer.C1.BirthDate"]/input')
self._fill(field, data)
You have FirstPage and SecondPage classes, which know how to do stuff, like how to fill the day input field. If you complete those two classes (and add the fill_month
and fill_year
methods) you can handle date fills with the same logic:
deffill_date(page, data):
page.fill_day(data[0])
page.fill_month(data[1])
page.fill_year(data[2])
first_page = FirstPage(driver)
second_page = SecondPage(driver)
if first_page_is_loaded:
fill_date(first_page, my_data)
else:
fill_date(second_page, my_data)
If the workflow of the pages is very similar, all you need to do is to use the right page object. You may handle minor differences by moving some logic to the page object, and use the same flow in the main logic.
Post a Comment for "Selelnium - Python | Date Of Birth Fields By Using Label"