def test_src_dst(self): # self.open_United() driver = self.driver time.sleep(2) # fill in src and dest airport in my case sfo and pvg try: src_airport = driver.find_element_by_id('bookFlightOriginInput') time.sleep(2) src_airport.send_keys(Keys.CONTROL, "a", Keys.BACKSPACE) print("Departing Airport input field has been clicked successfully.") time.sleep(2) action(driver).move_to_element(src_airport).send_keys("San Francisco").perform() time.sleep(2) except Exception as err: print(str(err)) try: dst_airport = driver.find_element_by_id("bookFlightDestinationInput") # dst_airport.click() time.sleep(2) dst_airport.click() time.sleep(1) print("Destination airport has been clicked successfully. ") action(driver).move_to_element(dst_airport).send_keys("Shanghai").perform() time.sleep(3) except Exception as err: print(str(err))
def main_fare_dropdown_(self): try: #fareDropdown is the dropdown menu element. CLicking on it simply opens up the dropdown menu to display the 3 dropdown options: economy, premium economy, and business/first #This dropdown menu is not coded like normal dropdowns, with an index. This is a graphic element that must be clicked by a mouse or manipulated using keys. There are no easy selectors for each option in the dropdown, so simulating clicks via keystrokes to navigate the dropdown are the only option. #If this method breaks, try adjusting the time.sleep first wait = WebDriverWait economy = Keys.ENTER premium = Keys.ARROW_DOWN, Keys.ENTER business = Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ENTER fareTypes = [economy, premium, business] fareDropdown = wait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'cabinType'))) fareDropdown.click() print('Fare/Cabin Dropdown Menu has been clicked successfully.') time.sleep(4) action(self.driver).send_keys(random.choice(fareTypes)).perform() print('Fare/Cabin type has been selected.') except Exception as err: print(str(err))
def destination_airport_input_field(self): airports = [] airportsfile = open(r'airports2.txt') for line in airportsfile: airports.append(line.strip()) try: DestinationAirportInputField = self.driver.find_element_by_id('bookFlightDestinationInput') DestinationAirportInputField.click() time.sleep(1) print('Destination Airport Input Field has been clicked successfully.') action(self.driver).move_to_element(DestinationAirportInputField).send_keys(random.choice(airports)).perform() print('The following keystrokes were entered into the Destination Airport Input Field: LAX') except Exception as err: print(str(err))
def move_by_offset(self, x, y): ''' :param x: 横坐标 :param y: 纵坐标 :return: 鼠标移动到(x,y)坐标位置 ''' return action(self.__driver).move_by_offset(x, y)
def departing_airport_input_field(self): airports = [] airportsfile = open(r'airports2.txt') for line in airportsfile: airports.append(line.strip()) try: DepartingAirportInputField = self.driver.find_element_by_id('bookFlightOriginInput') time.sleep(2) DepartingAirportInputField.send_keys(Keys.CONTROL, "a", Keys.BACKSPACE) print('Departing Airport Input Field has been clicked successfully.') time.sleep(2) action(self.driver).move_to_element(DepartingAirportInputField).send_keys(random.choice(airports)).perform() time.sleep(2) except Exception as err: print(str(err))
def drag_and_drop_by_offset(self, fromEle, x, y): ''' :param fromEle: 主元素 :param x: 横坐标 :param y: 纵坐标 :return: 以坐标的形式拖拽,x,y ''' return action(self.__driver).drag_and_drop_by_offset(fromEle, x, y)
def drag_and_drop(self, fromEle, toEle): ''' :param fromEle:主元素 :param toEle: 目标元素 :return: 从一个元素的位置,拖至另一个元素位置松开 ''' return action(self.__driver).drag_and_drop( self.find_element(fromEle), self.find_element(toEle)).perform()
def double_click(self, ele): ''' :param ele: ele :return: 在元素上边双击 ''' return action(self.__driver).double_click( self.find_element(ele)).perform()
def context_click(self, ele): ''' :param ele:ele :return: 在元素上右击 ''' return action(self.__driver).context_click( self.find_element(ele)).perform()
def click_and_hold(self, ele): ''' :param ele: ele :return: 操作鼠标按住不动到某个元素 ''' return action(self.__driver).click_and_hold( self.find_element(ele)).perform()
def move_to_element_with_offset(self, ele, x, y): ''' :param ele: 目标元素 :param x: 横坐标 :param y: 纵坐标 :return: 鼠标移动到目标元素,在移动到x,y上 ''' return action(self.__driver).move_to_element_with_offset( self.find_element(ele), x, y)
def test_num(self): # self.open_United() driver = self.driver time.sleep(2) # number of travellers try: travelers = driver.find_element_by_id('bookFlightModel.passengers') action(driver).move_to_element(travelers).click().perform() print('main travelers clicked successfully') except Exception as err: print(str(err)) time.sleep(2) try: peo = driver.find_element_by_id("NumOfAdults plusBtn") n(driver).move_to_element(peo).click().perform() print('Increased one adult traveler.') except Exception as err: print(str(err)) time.sleep(2)
def move_to_element(self, ele): ''' :param ele: 目标元素 :return: 鼠标移动到元素上 ''' return action(self.__driver).move_to_element(self.find_element(ele))
from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from UnitedSelectorClass import United_Flight_Booking chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('-incognito') driver = webdriver.Chrome(r"C:\drivers\chromedriver.exe", options=chrome_options) wait = WebDriverWait Test = United_Flight_Booking driver.get('http://www.united.com') print('United.com has been loaded in an incognito Chrome Window.') # fareDropdown is the dropdown menu element. CLicking on it simply opens up the dropdown menu to display the 3 dropdown options: economy, premium economy, and business/first economy = Keys.ENTER premium = Keys.ARROW_DOWN, Keys.ENTER business = Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ENTER fareTypes = [economy, premium, business] fareDropdown = wait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'cabinType'))) fareDropdown.click() time.sleep(5) action(driver).send_keys(random.choice(fareTypes)).perform() """ """