Best Python code snippet using selene_python
assignment_5.py
Source:assignment_5.py
1import urllib2from selenium import webdriver3driver = webdriver.Chrome()4driver.get("https://images.unsplash.com/photo-1579710758949-3ab36db30f1b?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDF8MHxzZWFyY2h8MXx8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1400")5driver.save_screenshot("nature1.png")6driver.get("https://images.unsplash.com/photo-1469474968028-56623f02e42e?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1400")7driver.save_screenshot("nature2.png")8driver.get("https://images.unsplash.com/photo-1433086966358-54859d0ed716?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8M3x8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1400")9driver.save_screenshot("nature3.png")10driver.get("https://images.unsplash.com/photo-1501854140801-50d01698950b?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NHx8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1400")11driver.save_screenshot("nature4.png")12driver.get("https://images.unsplash.com/photo-1447752875215-b2761acb3c5d?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Nnx8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1400")13driver.save_screenshot("nature5.png")14driver.get("https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1400")15driver.save_screenshot("nature6.png")16driver.get("https://images.unsplash.com/photo-1441974231531-c6227db76b6e?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8N3x8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1400")17driver.save_screenshot("nature7.png")18driver.get("https://images.unsplash.com/photo-1465146344425-f00d5f5c8f07?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTB8fG5hdHVyZXxlbnwwfHwwfHw%3D&auto=format&fit=crop&w=1400")19driver.save_screenshot("nature8.png")20driver.get("https://images.unsplash.com/photo-1647891940243-77a6483a152e?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDF8MHxzZWFyY2h8OHx8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1400")21driver.save_screenshot("nature9.png")22driver.get("https://images.unsplash.com/photo-1426604966848-d7adac402bff?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8OXx8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1400")23driver.save_screenshot("nature10.png")24driver.get("https://images.unsplash.com/photo-1587502536575-6dfba0a6e017?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDF8MHxzZWFyY2h8MXx8YmVhY2h8ZW58MHx8MHx8&auto=format&fit=crop&w=800")25driver.save_screenshot("beach1.png")26driver.get("https://images.unsplash.com/photo-1507525428034-b723cf961d3e?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8YmVhY2h8ZW58MHx8MHx8&auto=format&fit=crop&w=800")27driver.save_screenshot("beach2.png")28driver.get("https://images.unsplash.com/photo-1473116763249-2faaef81ccda?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8M3x8YmVhY2h8ZW58MHx8MHx8&auto=format&fit=crop&w=800")29driver.save_screenshot("beach3.png")30driver.get("https://images.unsplash.com/photo-1520454974749-611b7248ffdb?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Nnx8YmVhY2h8ZW58MHx8MHx8&auto=format&fit=crop&w=800")31driver.save_screenshot("beach4.png")32driver.get("https://images.unsplash.com/photo-1509233725247-49e657c54213?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8YmVhY2h8ZW58MHx8MHx8&auto=format&fit=crop&w=800")33driver.save_screenshot("beach5.png")34driver.get("https://images.unsplash.com/photo-1471922694854-ff1b63b20054?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NHx8YmVhY2h8ZW58MHx8MHx8&auto=format&fit=crop&w=800")35driver.save_screenshot("beach6.png")36driver.get("https://images.unsplash.com/photo-1519046904884-53103b34b206?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8N3x8YmVhY2h8ZW58MHx8MHx8&auto=format&fit=crop&w=800")37driver.save_screenshot("beach7.png")38driver.get("https://images.unsplash.com/photo-1564419320461-6870880221ad?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8OHx8YmVhY2h8ZW58MHx8MHx8&auto=format&fit=crop&w=800&q=60")39driver.save_screenshot("beach8.png")40driver.get("https://images.unsplash.com/photo-1506953823976-52e1fdc0149a?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8OXx8YmVhY2h8ZW58MHx8MHx8&auto=format&fit=crop&w=800")41driver.save_screenshot("beach9.png")42driver.get("https://images.unsplash.com/photo-1559494007-9f5847c49d94?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTB8fGJlYWNofGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=800")43driver.save_screenshot("beach10.png")44driver.get("https://images.unsplash.com/photo-1529778873920-4da4926a72c2?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8YW5pbWFsc3xlbnwwfHwwfHw%3D&auto=format&fit=crop&w=800")45driver.save_screenshot("animal1.png")46driver.get("https://images.unsplash.com/photo-1574870111867-089730e5a72b?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8YW5pbWFsc3xlbnwwfHwwfHw%3D&auto=format&fit=crop&w=800")47driver.save_screenshot("animal2.png")48driver.get("https://images.unsplash.com/photo-1591824438708-ce405f36ba3d?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8YW5pbWFsc3xlbnwwfHwwfHw%3D&auto=format&fit=crop&w=800")49driver.save_screenshot("animal3.png")50driver.get("https://images.unsplash.com/photo-1564349683136-77e08dba1ef7?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Nnx8YW5pbWFsc3xlbnwwfHwwfHw%3D&auto=format&fit=crop&w=800")51driver.save_screenshot("animal4.png")52driver.get("https://images.unsplash.com/photo-1583337130417-3346a1be7dee?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8M3x8YW5pbWFsc3xlbnwwfHwwfHw%3D&auto=format&fit=crop&w=800")53driver.save_screenshot("animal5.png")54driver.get("https://images.unsplash.com/photo-1456926631375-92c8ce872def?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8OHx8YW5pbWFsc3xlbnwwfHwwfHw%3D&auto=format&fit=crop&w=800")55driver.save_screenshot("animal6.png")56driver.get("https://images.unsplash.com/photo-1589656966895-2f33e7653819?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NHx8YW5pbWFsc3xlbnwwfHwwfHw%3D&auto=format&fit=crop&w=800")57driver.save_screenshot("animal7.png")58driver.get("https://images.unsplash.com/photo-1474511320723-9a56873867b5?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTB8fGFuaW1hbHN8ZW58MHx8MHx8&auto=format&fit=crop&w=800")59driver.save_screenshot("animal8.png")60driver.get("https://images.unsplash.com/photo-1484406566174-9da000fda645?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8OXx8YW5pbWFsc3xlbnwwfHwwfHw%3D&auto=format&fit=crop&w=800")61driver.save_screenshot("animal9.png")62driver.get("https://images.unsplash.com/photo-1574068468668-a05a11f871da?crop=entropy&cs=tinysrgb&fm=jpg&ixlib=rb-1.2.1&q=60&raw_url=true&ixid=MnwxMjA3fDB8MHxzZWFyY2h8N3x8YW5pbWFsc3xlbnwwfHwwfHw%3D&auto=format&fit=crop&w=800")63driver.save_screenshot("animal10.png")...
base_page.py
Source:base_page.py
...8class BasePage:9 """å°è£
ææ页é¢å
Œ
±çéä¸å¡æä½"""10 def __init__(self,driver):11 self.driver = driver12 def save_screenshot(self):13 """ä¿åæªå¾"""14 screenshot_time = time.strftime('%Y-%m-%d %H_%M_%S') #æªå¾çæ¶é´15 file_name = screenshot_dir + '{0}.png'.format(screenshot_time) #æªå¾ä¿åçè·¯å¾åå称16 try:17 self.driver.save_screenshot(file_name)18 MyLogger().info('æªå¾æåï¼å¾çè·¯å¾ä¸º{0}'.format(file_name))19 except:20 MyLogger().error('æªå¾å¤±è´¥ï¼')21 def wait_element_visible(self,locator,timeout=20):22 """çå¾
å
ç´ å¯è§"""23 MyLogger().info('çå¾
{0}å
ç´ å¯è§'.format(locator))24 try:25 WebDriverWait(self.driver,timeout).until(EC.visibility_of_element_located(locator))26 except:27 self.save_screenshot()28 MyLogger().error('çå¾
å
ç´ å¯è§å¤±è´¥ï¼')29 raise30 def wait_element_present(self,locator,timeout=20):31 """çå¾
å
ç´ åå¨"""32 MyLogger().info('çå¾
{0}å
ç´ åå¨'.format(locator))33 try:34 WebDriverWait(self.driver,timeout).until(EC.presence_of_element_located(locator))35 except:36 self.save_screenshot()37 MyLogger().error('çå¾
å
ç´ åå¨å¤±è´¥ï¼')38 raise39 def get_element(self,locator):40 """æ¥æ¾å
ç´ """41 MyLogger().info('æ¥æ¾å
ç´ ï¼{0}'.format(locator))42 try:43 return self.driver.find_element(*locator)44 except:45 self.save_screenshot()46 MyLogger().error('æ¥æ¾å
ç´ å¤±è´¥ï¼')47 raise48 def click_element(self,locator):49 """ç¹å»å
ç´ æä½"""50 MyLogger().info('ç¹å»å
ç´ {0}'.format(locator))51 element = self.get_element(locator)52 try:53 element.click()54 except:55 self.save_screenshot()56 MyLogger().error('ç¹å»å
ç´ å¤±è´¥ï¼')57 raise58 def input_text(self,locator,text):59 """è¾å
¥ææ¬æä½"""60 MyLogger().info('å
ç´ {0}è¾å
¥{1}'.format(locator,text))61 element = self.get_element(locator)62 try:63 element.send_keys(text)64 except:65 self.save_screenshot()66 MyLogger().error('è¾å
¥ææ¬å¤±è´¥ï¼')67 raise68 def get_element_attribute(self,locator,attribute):69 """è·åå
ç´ å±æ§"""70 MyLogger().info('è·åå
ç´ {0}ç{1}å±æ§'.format(locator,attribute))71 element = self.get_element(locator)72 try:73 return element.get_attribute(attribute)74 except:75 self.save_screenshot()76 MyLogger().error('è·åå
ç´ å±æ§å¤±è´¥ï¼')77 raise78 def get_element_text(self,locator):79 """è·åå
ç´ ææ¬å
容"""80 MyLogger().info('è·åå
ç´ {0}çææ¬å
容'.format(locator))81 element = self.get_element(locator)82 try:83 return element.text84 except:85 self.save_screenshot()86 MyLogger().error('è·åå
ç´ ææ¬å
容失败ï¼')87 raise88 def wait_alert_visible(self,timeout=20):89 """çå¾
alertå¼¹æ¡åºç°"""90 MyLogger().info('çå¾
alertå¼¹æ¡åºç°')91 try:92 WebDriverWait(self.driver,timeout).until(EC.alert_is_present())93 except:94 self.save_screenshot()95 MyLogger().error('çå¾
alertå¼¹æ¡åºç°å¤±è´¥ï¼')96 raise97 def alert_operation(self,operation='accept'):98 """alertå¤çæä½ï¼é»è®¤åæ"""99 MyLogger().info('alertå¼¹æ¡å¤ç')100 try:101 alert = self.driver.switch.to_alert102 if operation == 'accept':103 alert.accept()104 elif operation == 'dismiss':105 alert.dismiss()106 except:107 self.save_screenshot()108 MyLogger().error('alertå¼¹æ¡å¤ç失败ï¼')109 raise110 def switch_iframe(self,iframe_name,timeout=20):111 """iframeåæ¢æä½"""112 MyLogger().info('å
ç´ {0}æä½iframeåæ¢'.format(iframe_name))113 try:114 WebDriverWait(self.driver,timeout).until(EC.frame_to_be_available_and_switch_to_it(iframe_name))115 except:116 self.save_screenshot()117 MyLogger().error('iframeåæ¢å¤±è´¥ï¼')118 raise119 def scrollbar_operation(self):120 """æ»å¨æ¡å¤ç--移å¨å°é¡µé¢åºé¨"""121 MyLogger().info('å°æ»å¨æ¡ç§»å¨å°é¡µé¢åºé¨')122 try:123 self.driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")124 except:125 self.save_screenshot()126 MyLogger().error('æ»å¨æ¡ç§»å¨å°é¡µé¢åºé¨å¤±è´¥ï¼')...
save_screenshot.py
Source:save_screenshot.py
1from screenplay import Task, Actor, log_message2from screenplay_selenium.actions import save_screenshot_to_file3from os import path4class save_screenshot(Task):5 path = None6 index = 07 def __init__(self):8 super().__init__()9 save_screenshot.index += 110 self._index = save_screenshot.index11 @log_message("Save screenshot '{self._index}.png'")12 def perform_as(self, actor: Actor):13 assert save_screenshot.path is not None, "capture_screenshot.path not set before trying to capture a screenshot"14 filename = path.join(save_screenshot.path, str(self._index) + '.png')15 actor.attempts_to(16 save_screenshot_to_file(filename)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!