Best Python code snippet using SeleniumBase
online_shop.py
Source:online_shop.py
1# ååæ¥è©¢å¨~2from selenium import webdriver3import pyautogui4# edge ç webdriver è¦ä¾ç
§çæ¬ä¾åæ´æ°ï¼ï¼5# è®ä½¿ç¨è
輸å
¥ä»è¦æ¥çæ±è¥¿(ç®åæ使ç¨ççº"è¦ç®")6print("----------")7want = input("欲æ¥è©¢çååï¼")8if (want != ""):9 money_range = input("æ¯å¦è¦éåå¹æ ¼ç¯å(å¡«å
¥<æ¯> or <å¦>)ï¼")10 if money_range == "æ¯":11 miniprice = input("æéçæ<ä½>å¹ä½ï¼")12 maxprice = input("æéçæ<é«>å¹ä½ï¼")13 elif money_range == "å¦":14 skip = "skip"15 else :16 print("æ輸å
¥çæ¹å¼æ誤")17 store = input("欲å¨åªè£¡æå°(1.è¦ç®ã2.momoã3.é²å¤©ã4.以ä¸ä¸è
çè¦)---輸å
¥ç·¨èï¼")18 if (store == "1") or (store == "1."):19 if money_range == "å¦":20 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")21 driver.maximize_window22 driver.get(f"https://shopee.tw/search?keyword={want}")23 elif money_range == "æ¯":24 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")25 driver.maximize_window26 driver.get(f"https://shopee.tw/search?keyword={want}&maxPrice={maxprice}&minPrice={miniprice}")27 else :28 print("æè«æ±çæ¹å¼ä¸å°ï¼ä½ä»å¹«ä½ æ¥æ¾åå")29 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")30 driver.maximize_window31 driver.get(f"https://shopee.tw/search?keyword={want}")32 print("å·²æååå©æå°")33 elif (store == "2") or (store == "2."):34 if money_range == "å¦":35 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")36 driver.maximize_window37 driver.get(f"https://www.momoshop.com.tw/search/searchShop.jsp?keyword={want}")38 elif money_range == "æ¯":39 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")40 driver.maximize_window41 driver.get(f"https://www.momoshop.com.tw/search/searchShop.jsp?keyword={want}&_advPriceS={miniprice}&_advPriceE={maxprice}")42 else :43 print("æè«æ±çæ¹å¼ä¸å°ï¼ä½ä»å¹«ä½ æ¥æ¾åå")44 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")45 driver.maximize_window46 driver.get(f"https://www.momoshop.com.tw/search/searchShop.jsp?keyword={want}")47 print("å·²æååå©æå°")48 elif (store == "3") or (store == "3."):49 if money_range == "å¦":50 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")51 driver.maximize_window52 driver.get(f"https://www.ruten.com.tw/find/?q={want}")53 elif money_range == "æ¯":54 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")55 driver.maximize_window56 driver.get(f"https://www.ruten.com.tw/find/?q={want}&prc.now={miniprice}-{maxprice}")57 else :58 print("æè«æ±çæ¹å¼ä¸å°ï¼ä½ä»å¹«ä½ æ¥æ¾åå")59 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")60 driver.maximize_window61 driver.get(f"https://www.ruten.com.tw/find/?q={want}")62 print("å·²æååå©æå°")63 elif (store == "4") or (store == "4."):64 page = 165 if money_range == "å¦":66 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")67 driver.maximize_window68 driver.get(f"https://shopee.tw/search?keyword={want}")69 #[1]ï¼æä¸ Ctrl + T ä¾æ°å¢åé ï¼èä¸è¦æé
pyautogui70 pyautogui.hotkey('ctrl', 't', interval=0.1) #[1]71 #[2]ï¼åæç覽å¨çwindow handle72 driver.switch_to.window(driver.window_handles[page]) #[2]73 #[3]ï¼pageè¨å¾+174 page+=1 #[3]75 driver.get(f"https://www.momoshop.com.tw/search/searchShop.jsp?keyword={want}")76 pyautogui.hotkey('ctrl', 't', interval=0.1)77 driver.switch_to.window(driver.window_handles[page])78 driver.get(f"https://www.ruten.com.tw/find/?q={want}")79 elif money_range == "æ¯":80 driver = webdriver.Edge(executable_path="X:\\edge_driver\\msedgedriver.exe")81 driver.maximize_window82 driver.get(f"https://shopee.tw/search?keyword={want}&maxPrice={maxprice}&minPrice={miniprice}")83 pyautogui.hotkey('ctrl', 't', interval=0.1)84 driver.switch_to.window(driver.window_handles[page])85 page+=186 driver.get(f"https://www.momoshop.com.tw/search/searchShop.jsp?keyword={want}&_advPriceS={miniprice}&_advPriceE={maxprice}")87 pyautogui.hotkey('ctrl', 't', interval=0.1)88 driver.switch_to.window(driver.window_handles[page])89 driver.get(f"https://www.ruten.com.tw/find/?q={want}&prc.now={miniprice}-{maxprice}")90 print("å·²æååå©æå°")91 else :92 print("ç®åç¡æä¾æ¤é¡ååä¾æå°ï¼æè
æ¯æ¨ç輸å
¥æ誤~")...
Base.py
Source:Base.py
...1516 self.driver = ''17 if Web_Browser == "Mozilla Firefox":18 self.driver = webdriver.Firefox()19 self.driver.maximize_window()2021 elif Web_Browser == "Google Chrome":22 self.driver =\23 webdriver.Chrome(24 pathToChromedriver25 )26 self.driver.maximize_window()2728 elif Web_Browser == "Internet Explorer 32":29 self.driver =\30 webdriver.Ie(31 pathToIEDriverServer_Win3232 )33 self.driver.maximize_window()3435 elif Web_Browser == "Internet Explorer 64":36 self.driver =\37 webdriver.Ie(38 pathToIEDriverServer_x6439 )40 self.driver.maximize_window()4142 elif Web_Browser == "Android Chrome":43 capabilities = {44 'chromeOptions': {45 'androidPackage': 'com.android.chrome',46 }47 }48 self.driver = webdriver.Remote('http://' + server + ':9515/wd/hub',49 capabilities)5051 elif Web_Browser == "Android Chrome Remote":52 capabilities = {53 'chromeOptions': {54 'androidPackage': 'com.android.chrome',55 }56 }57 self.driver = webdriver.Remote(remoteURL,58 capabilities)5960 elif Web_Browser == "Mozilla Firefox Grid local":61 self.driver = webdriver.Remote(62 command_executor=gridURL,63 desired_capabilities={64 "browserName": "firefox",65 "version": "38.0.1",66 })67 self.driver.maximize_window()6869 elif Web_Browser == "Mozilla Firefox Grid remote":70 self.driver = webdriver.Remote(71 command_executor=gridURL,72 desired_capabilities={73 "browserName": "firefox",74 "version": "38.0.1 remote",75 })76 self.driver.maximize_window()7778 elif Web_Browser == "Google Chrome Grid local":79 self.driver = webdriver.Remote(80 command_executor=gridURL,81 desired_capabilities={82 "browserName": "chrome",83 "version": "42.0.2311.152 m (64-bit)",84 })85 self.driver.maximize_window()8687 elif Web_Browser == "Google Chrome Grid remote":88 self.driver = webdriver.Remote(89 command_executor=gridURL,90 desired_capabilities={91 "browserName": "chrome",92 "version": "42.0.2311.152 m remote",93 })94 self.driver.maximize_window()9596 elif Web_Browser == "Internet Explorer Grid local":97 self.driver = webdriver.Remote(98 command_executor=gridURL,99 desired_capabilities={100 "browserName": "internet explorer",101 "version": "11.0 X64",102 })103 self.driver.maximize_window()104105 elif Web_Browser == "Internet Explorer Grid remote":106 self.driver = webdriver.Remote(107 command_executor=gridURL,108 desired_capabilities={109 "browserName": "internet explorer",110 "version": "11.0 Win32",111 })112 self.driver.maximize_window()113114 elif Web_Browser == "Android Chrome Grid local":115 self.driver = webdriver.Remote(116 command_executor=gridURL,117 desired_capabilities={118 'chromeOptions': {119 'androidPackage': 'com.android.chrome',120 },121 "browserName": "chrome",122 "platform": "WINDOWS",123 "version": "android local"124 })125126 elif Web_Browser == "Android Chrome Grid remote":
...
conftest.py
Source:conftest.py
...4"""5@pytest.fixture6def setUp():7 driver = webdriver.Chrome('D:\driver\chromedriver_win32\chromedriver.exe')8 driver.maximize_window()9 return driver10 11 """12'''13@pytest.fixture14def setUp():15 #driver = webdriver.Chrome('D:\driver\chromedriver_win32\chromedriver.exe')16 driver = webdriver.Firefox(executable_path="D:\\driver\\geckodriver.exe")17 driver.maximize_window()18 return driver19 20 '''21'''22@pytest.fixture23def setUp():24 # driver = webdriver.Chrome('D:\driver\chromedriver_win32\chromedriver.exe')25 driver = webdriver.Ie(executable_path="D:\\driver\\IEDriverServer.exe")26 driver.maximize_window()27 return driver28 29 '''30# @pytest.fixture31# def setUp(browser):32# if browser == 'chrome':33# #driver = webdriver.Chrome('D:\driver\chromedriver_win32\chromedriver.exe')34# # D:\driver\driver_version\chromedriver_win32 (1)\chromedriver.exe35#36# driver = webdriver.Chrome('D:\driver\driver_version\chromedriver_win32 (1)\chromedriver.exe')37# driver.maximize_window()38# print("launching chrome driver")39#40# elif browser == 'firefox':41# driver = webdriver.Firefox(executable_path="D:\\driver\\geckodriver.exe")42# driver.maximize_window()43# print("launching firefox driver")44#45# else:46# driver = webdriver.Ie(executable_path="D:\\driver\\IEDriverServer.exe")47# driver.maximize_window()48# return driver49#50#51# def pytest_addoption(parser):52# parser.addoption("--browser")53#54#55# @pytest.fixture56# def browser(request):57# return request.config.getoption("--browser")58########### pytest HTML Report ################59'''60# It is hook for Adding Environment info to HTML Report61def pytest_configure(config):62 config._metadata['Project Name'] = 'nop Commerce'63 config._metadata['Module Name'] = 'Customers'64 config._metadata['Tester'] = 'Pavan'65# It is hook for delete/Modify Environment info to HTML Report66@pytest.mark.optionalhook67def pytest_metadata(metadata):68 metadata.pop("JAVA_HOME", None)69 metadata.pop("Plugins", None)70 71 '''72@pytest.fixture73def setUp(browser):74 if browser == 'chrome':75 parent_dir = os.getcwd()76 directory = '\\configuration\\chromedriver.exe'77 path = parent_dir + directory78 driver = webdriver.Chrome(path)79 driver.maximize_window()80 print("launching chrome driver")81 elif browser == 'firefox':82 parent_dir = os.getcwd()83 directory = '\\configuration\\geckodriver.exe'84 path = parent_dir + directory85 driver = webdriver.Firefox(executable_path=path)86 driver.maximize_window()87 print("launching firefox driver")88 else:89 parent_dir = os.getcwd()90 directory = '\\configuration\\IEDriverServer.exe'91 path = parent_dir + directory92 driver = webdriver.Ie(executable_path=path)93 driver.maximize_window()94 print("launching IE driver")95 return driver96def pytest_addoption(parser):97 parser.addoption("--browser")98@pytest.fixture99def browser(request):...
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!!