class EdgeTest(unittest.TestCase): def setUp(self): WebDriverFactory().setup_edgedriver() from msedge.selenium_tools import Edge, EdgeOptions options = EdgeOptions() options.use_chromium = True options.set_capability('platform', 'MAC' if OS_NAME == 'MAC' else 'WINDOWS') self.driver1 = Edge(options=options) self.driver2 = Edge(options=options) self.driver1.maximize_window() self.wait1 = WebDriverWait(self.driver1, 5) self.wait2 = WebDriverWait(self.driver2, 5) def tearDown(self): self.driver2.quit() self.driver1.quit() def test_something(self): self.driver1.get('https://www.google.com') logger.info(self.driver1.title) self.assertIn(self.driver1.title, 'Google') self.driver1.get('https://www.naver.com') news_btn1 = self.driver1.find_element(By.CSS_SELECTOR, '.link_news') news_btn1.click() logger.info(self.driver1.current_url) self.assertTrue(self.wait1.until(expected_conditions.url_contains('https://news.naver.com'))) self.driver2.get('https://www.google.com') logger.info(self.driver2.title) self.assertIn(self.driver2.title, 'Google') self.driver2.get('https://www.naver.com') news_btn2 = self.driver2.find_element(By.CSS_SELECTOR, '.link_join') news_btn2.click() logger.info(self.driver2.current_url) self.assertTrue(self.wait2.until(expected_conditions.url_contains('https://nid.naver.com')))
def codigo(link, navegador): cont = 0 x = 1 y = 1 split = link.split('q=') print(split) print(navegador) if navegador == 2: options = EdgeOptions() options.use_chromium = True driver = Edge(options=options) driver.maximize_window() #print(link) driver.get("https://scholar.google.com.br/scholar?start=0&q={}".format( split[1])) root = os.getcwd() wait = WebDriverWait(driver, 10) while (True): try: os.mkdir('planilhas') except FileExistsError: os.chdir(f'{root}\\planilhas') wb = Workbook() planilha = wb.worksheets[0] items = wait.until( EC.visibility_of_all_elements_located( (By.CSS_SELECTOR, '.gs_rt a'))) for item in items: nome_artigo = item.text link_artigo = item.get_attribute('href') print('Artigo: ', nome_artigo, '\nLink: ', link_artigo, '\n') planilha['A{}'.format(x)] = nome_artigo planilha['B{}'.format(y)] = link_artigo botao = driver.find_element_by_partial_link_text('Mais') botao.click() wb.save("planilha.xlsx") x += 1 y += 1 return
def open_browser(env, browser='chrome', incognito=True): driver = None if browser == "chrome": chrome_options = webdriver.ChromeOptions() if incognito: chrome_options.add_argument('--incognito') driver = webdriver.Chrome(executable_path=chrome_driver_path, chrome_options=chrome_options) elif browser == "msedge": edge_options = EdgeOptions() edge_options.use_chromium = True if incognito: edge_options.add_argument('-inprivate') driver = Edge(executable_path=msedge_driver_path, options=edge_options) elif browser == "firefox": firefox_options = webdriver.FirefoxOptions() if incognito: firefox_options.add_argument('--incognito') driver = webdriver.Firefox(executable_path=firefox_driver_path, options=firefox_options) elif browser == "ie": driver = webdriver.Ie(executable_path=ie_driver_path) # selenium grid # driver = webdriver.Remote(command_executor='http://10.22.40.234:5555/wd/hub', # desired_capabilities=DesiredCapabilities.INTERNETEXPLORER) elif browser == "safari": driver = webdriver.Safari() # 获取web配置 with open(web_config_path, 'r', encoding='utf-8') as file: data = yaml.load(file, Loader=yaml.FullLoader) # 以下是一个示例,基于config/web_config.yaml文件做的配置 if env == "msit": url = data["portal"]['msit'] Logger.info("Open Url: %s", url) driver.get(url) if env == "srol1": url = data["portal"]['srol1'] Logger.info("Open Url: %s", url) driver.get(url) if env == "srol2": url = data["portal"]['srol2'] Logger.info("Open Url: %s", url) driver.get(url) if env == "ppe": url = data["portal"]['ppe'] Logger.info("Open Url: %s", url) driver.get(url) if env == "refe": url = data["portal"]['refe'] Logger.info("Open Url: %s", url) driver.get(url) elif env == '': driver = None driver.maximize_window() driver.implicitly_wait(data['implicitly_wait']) return driver
def browserOpen(self, driver): ''' # Which browser do you want to test? He has to go to the config.ini file and uncomment browser name. :param driver: :return: ''' config = ConfigParser() file_path = os.path.dirname(os.path.abspath('')) + "/Config/config.ini" config.read(file_path) browser = config.get("browserType", "browserName") log.info(f"You had select {browser} browser.") url = config.get("testUrl", "URL") log.info(f"The test url is: {url}") # check browser name right or wrong print("--------Open browser--------") if browser == "Chrome": driver = webdriver.Chrome(self.chrome_driver_path) log.info("Start chrome browser.") elif browser == "Firefox": driver = webdriver.Firefox( executable_path=self.firefox_driver_path) log.info("Start Firefox browser.") elif browser == "IE": driver = webdriver.Ie(executable_path=self.ie_driver_path) log.info("Start IE browser.") elif browser == "Safari": driver = webdriver.Safari( executable_path="/use/local/bin/safaridriver") log.info("Start safari browser.") elif browser == "Edge": # driver = webdriver.Edge(executable_path=self.edge_driver_path) # edge_options = EdgeOptions() # edge_options.use_chromium = True # if we miss this line, we can't make Edge headless # # A little different from Chrome cause we don't need two lines before 'headless' and 'disable-gpu' # edge_options.add_argument('headless') # edge_options.add_argument('disable-gpu') # # driver = Edge(executable_path=self.edge_driver_path, options=edge_options) driver = Edge(executable_path=self.edge_driver_path) log.info("Start Edge browser.") driver.get(url) log.info(f"Open url: {url}") driver.maximize_window() log.info("Maximize the current window.") driver.implicitly_wait(10) log.info("Set implicitly wait 10 seconds.") return driver
class HeaderText(unittest.TestCase): def setUp(self): options = EdgeOptions() options.use_chromium = True options.binary_location = "C:\\Program Files (x86)\\Microsoft\\Edge Dev\\Application\\msedge.exe" dir = os.path.dirname(os.path.realpath(__file__)) edge_driver_path = dir + "\\edgedriver_win64\\msedgedriver.exe" self.driver = Edge(options=options, executable_path=edge_driver_path) self.driver.implicitly_wait(30) self.driver.maximize_window() self.driver.get("http://localhost:4200") def test_HeaderText(self): headerText = self.driver.find_element_by_css_selector("h1").get_attribute("innerText") self.assertEqual("todos", headerText) def tearDown(self): self.driver.quit()
class AddAToDoText(unittest.TestCase): def setUp(self): options = EdgeOptions() options.use_chromium = True options.binary_location = "C:\\Program Files (x86)\\Microsoft\\Edge Dev\\Application\\msedge.exe" dir = os.path.dirname(os.path.realpath(__file__)) edge_driver_path = dir + "\\edgedriver_win64\\msedgedriver.exe" self.driver = Edge(options=options, executable_path=edge_driver_path) self.driver.implicitly_wait(30) self.driver.maximize_window() self.driver.get("http://*****:*****@class='toggle']/following-sibling::label").get_attribute("innerText") self.assertEqual("The test is adding this todo", addedToDoText) def tearDown(self): self.driver.quit()
def browser(request): maximized = request.config.getoption("--maximized") headless = request.config.getoption("--headless") browser = request.config.getoption("--browser") if browser == "chrome": options = webdriver.ChromeOptions() if headless: options.headless = True driver = webdriver.Chrome(options=options) if browser == "firefox": options = webdriver.FirefoxOptions() if platform.system() == "Windows": local_data = os.getenv('LOCALAPPDATA', None) options.binary_location = os.path.join( local_data, r"Mozilla Firefox\firefox.exe") if headless: options.headless = True driver = webdriver.Firefox(options=options) if browser == "edge": options = EdgeOptions() if headless: options.use_chromium = True options.headless = True driver = Edge( executable_path=r"C:/projects/drivers/MicrosoftWebDriver.exe", options=options ) if maximized: driver.maximize_window() def fin(): driver.quit() print(f'Test {request.node.name} is over') request.addfinalizer(fin) return driver
search_term = '' describe_list = ['sucks','great'] data = [] cutoff_date = datetime(2020,9,13) for x in qb_list: for j in describe_list: search_term = x+" "+j # create instance of web driver options = EdgeOptions() options.use_chromium = True driver = Edge(options=options) # navigate to login screen driver.get('https://www.twitter.com/login') driver.maximize_window() username = driver.find_element_by_xpath('//input[@name="session[username_or_email]"]') username.send_keys(user) password = driver.find_element_by_xpath('//input[@name="session[password]"]') password.send_keys(my_password) password.send_keys(Keys.RETURN) sleep(1) # find search input and search for term search_input = driver.find_element_by_xpath('//input[@aria-label="Search query"]') search_input.send_keys(search_term) search_input.send_keys(Keys.RETURN) sleep(1)
try: # Calculate action delay based on ping. Cannot be below 1, Might change. host=socket.gethostbyname("kahoot.it") before=time.perf_counter() time.sleep(0.25) s=socket.create_connection((host, 80), 2) after=time.perf_counter() pingms=after-before pingms=round(pingms,2)+1 except: pingms=2 print("-Calculated action delay: "+str(pingms)) # Maximize window for no real reason browser.maximize_window() for i in range(int(nb)): passed=passed+1 try: # Open browser. if firstTime == True: browser.get("https://kahoot.it/") else: browser.execute_script("window.open('');") browser.switch_to.window(browser.window_handles[total]) browser.get("https://kahoot.it/") #if firstTime == False: # wait=WebDriverWait(browser, 3) # alert=wait.until(EC.alert_is_present()) # alert.accept()
class TwitterBot(): def __init__(self): self.driver = Edge() self.driver.maximize_window() self.driver.get('https://twitter.com') self.driver.implicitly_wait(3) def goToTwitter(self): self.driver.get('https://twitter.com') def login(self): self.driver.find_element_by_xpath("//a[@href='/login']").click() #I used sleep because before this time there is another instance of an element named like below. #It is crucial to get the right element in order to interact with it. sleep(1) self.driver.find_element_by_xpath( "//input[@name='session[username_or_email]']").send_keys(username) self.driver.find_element_by_xpath( "//input[@name='session[password]']").send_keys(password) self.driver.find_element_by_xpath( "//div[@data-testid='LoginForm_Login_Button']").click() def basicSearch(self, topic): self.driver.find_element_by_xpath( "//input[@data-testid='SearchBox_Search_Input']").send_keys(topic) self.driver.find_element_by_xpath( "//input[@data-testid='SearchBox_Search_Input']").submit() def advancedSearch(self, exact, any, none, hashtags, dateFrom, dateTo): finalSearch = '' #This is to accommodate for different search types that a user might want. if exact != None: finalSearch += '"' + exact + '" ' if any != None: finalSearch += '(' + any + ') ' if none != None: finalSearch += '-' + none + ' ' if hashtags != None: finalSearch += '(#' + hashtags + ') ' if dateTo != None: finalSearch += 'until:' + dateTo + ' ' if dateFrom != None: finalSearch += 'since:' + dateFrom + ' ' self.driver.find_element_by_xpath( "//input[@data-testid='SearchBox_Search_Input']").send_keys( finalSearch) self.driver.find_element_by_xpath( "//input[@data-testid='SearchBox_Search_Input']").submit() def scrapeTweets(self, desiredNum): allLines = '' oldDataLines = [] dataLines = ['init'] tweetsFile = open('tweets.csv', 'w') #I included this array to help clean data later dirtyArray = [ 'Quote Tweet', 'Promoted', 'Show this thread', '', '\n', ' ' ] numDataLines = 0 while numDataLines < desiredNum and oldDataLines != dataLines: oldDataLines = dataLines sleep(1) #all these are different types of data that I do not want to pick up. dirtyData = self.driver.find_elements_by_xpath( "//div[@class='css-1dbjc4n r-1d09ksm r-18u37iz r-1wbh5a2']") dirtyData2 = self.driver.find_elements_by_xpath( "//div[@class = 'css-1dbjc4n r-18u37iz r-1wtj0ep r-156q2ks r-1mdbhws']" ) dirtyData3 = self.driver.find_elements_by_xpath( "//div[contains(text(),'Replying to')]") dirtyData4 = self.driver.find_elements_by_xpath( "//div[@role = 'blockquote']") #adding all the dirty data into one array for dirt in dirtyData2: dirtyData.append(dirt) for dirt in dirtyData3: dirtyData.append(dirt) for dirt in dirtyData4: dirtyData.append(dirt) #the data is stored with strings with many lines so I split the strings up by line and have an array where each index is one lin dirtyLines = [] for dirt in dirtyData: dirt = dirt.text chunks = dirt.split('\n') for chunk in chunks: dirtyLines.append(chunk) #this includes dirty data that will be weeded out later data = self.driver.find_elements_by_xpath( "//div[@data-testid='tweet']") #same thing I did with dirtyLines dataLines = [] for datapoint in data: datapoint = datapoint.text chunks = datapoint.split('\n') for chunk in chunks: dataLines.append(chunk) #I check oldDataLines as well to avoid redundancy for line in dataLines: if line not in dirtyLines and line not in oldDataLines and line not in dirtyArray: if numDataLines >= desiredNum: break try: noPunctuationLine = re.sub(r'[^\w\s]', '', line) tweetsFile.write(noPunctuationLine) tweetsFile.write("\n") allLines += line numDataLines += 1 except Exception: print('This data point not encodable.') height = self.driver.execute_script( "return document.documentElement.scrollHeight") self.driver.execute_script("window.scrollTo(0, " + str(height) + ");") tweetsFile.close() return allLines
def scrape(secure=False): options = EdgeOptions() options.use_chromium = True driver = Edge(options=options) query = input("▁ ▂ ▄ ▅ ▆ ▇ █ 𝐄𝐧𝐭𝐞𝐫 𝐭𝐡𝐞 𝐓𝐞𝐱𝐭 𝐭𝐨 𝐬𝐞𝐚𝐫𝐜𝐡 █ ▇ ▆ ▅ ▄ ▂ ▁\n\n ") print("\n𝘚𝘵𝘢𝘳𝘵𝘦𝘥 𝘚𝘤𝘳𝘢𝘱𝘪𝘯𝘨 ↦↦↦↦↦↦↦↦↦↦") print("\nPlease Wait ............\n") driver.get("https://www.twitter.com/login") driver.maximize_window() username = driver.find_element_by_xpath( '//input[@name="session[username_or_email]"]') username.send_keys("*****@*****.**") #password=getpass() userpas = driver.find_element_by_xpath( '//input[@name="session[password]"]') userpas.send_keys('-----') userpas.send_keys(Keys.RETURN) sleep(2) if secure: username = driver.find_element_by_xpath( '//input[@name="session[username_or_email]"]') username.send_keys("031-----") userpas = driver.find_element_by_xpath( '//input[@name="session[password]"]') userpas.send_keys('----') userpas.send_keys(Keys.RETURN) sleep(2) search = driver.find_element_by_xpath( '//input[@aria-label="Search query"]') search.send_keys('"پاک فوج" lang:ur -filter:links filter:replies') search.send_keys(Keys.RETURN) sleep(1.5) driver.find_element_by_link_text("Latest").click() data = [] tweet_ids = set() last_position = driver.execute_script("return window.pageYOffset;") scrolling = True while scrolling: posts = driver.find_elements_by_xpath('//div[@data-testid="tweet"]') for post in posts[-15:]: tweet = scrap_tweets(post) if tweet: tweet_id = "".join(tweet) if tweet_id not in tweet_ids: tweet_ids.add(tweet_id) data.append(tweet) scroll_attempt = 0 while True: driver.execute_script( "window.scrollTo(0,document.body.scrollHeight);") sleep(1) curr_position = driver.execute_script("return window.pageYOffset;") if last_position == curr_position: scroll_attempt += 1 if scroll_attempt >= 3: scrolling = False break else: sleep(2) else: last_position = curr_position break return data
def main(): args = sys.argv f = open(args[4], "r") Lines = f.readlines() names, profession, nationality, job = [], [], [], [] for line in Lines: array = line.split(",") names.append(array[0]) profession.append(array[1]) nationality.append(array[2]) job.append(array[3].replace("\n", "")) for name in names: print("Query:", name, ".\nProcessing...") user = '******' search_term = f'{name} filter:verified' options = EdgeOptions() options.use_chromium = True driver = Edge(options=options) driver.get('https://www.twitter.com/login') driver.maximize_window() sleep(2) username = driver.find_element_by_xpath( '//input[@name="session[username_or_email]"]') username.send_keys(user) password = driver.find_element_by_xpath( '//input[@name="session[password]"]') password.send_keys('donkey123') password.send_keys(Keys.RETURN) sleep(1) search_input = driver.find_element_by_xpath( '//input[@aria-label="Search query"]') search_input.send_keys(search_term) search_input.send_keys(Keys.RETURN) sleep(1) driver.find_element_by_link_text('People').click() sleep(3) driver.find_element_by_xpath( '//div[@class="css-1dbjc4n r-j7yic r-qklmqi r-1adg3ll r-1ny4l3l"]' ).click() sleep(3) data = [] tweet_data = [] start = 0 end = 500 for i in range(0, 5): sleep(1) cards = driver.find_elements_by_xpath( '//div[@data-testid="tweet"]') card = cards[i] tweet = get_tweet_data(card) for card in cards: data = get_tweet_data(card) if data: tweet_data.append(data) driver.execute_script(f'window.scrollTo({start},{end});') start += 500 end += 500 driver.close() tweets = set(tweet_data) write_to_csv(name, tweets) df = pd.read_csv(f'{name}.csv') Twitter_sentiment = Twitter_sentiment_model(df) Twitter_toxic = Twitter_toxic_model(df) Big5 = Big5_model(df) create_report(name, tweets, Twitter_sentiment, Twitter_toxic, Big5)
def main(): try: print("ウェブドライバーを立ち上げています・・・") port = str(args.port[0]) load_delay_time = args.load_delay_time[0] options = EdgeOptions() options.use_chromium = True driver = Edge(options=options) driver.maximize_window() if len(port) != 4: print("入力した番号は4桁ではないです。4桁のポート番号を記入してください。") quit() print("ページを開いています・・・") driver.get(f"http://127.0.0.1:{port}") print(f"ページの読み込みのため{str(load_delay_time)}秒待機します・・・") for i in range(load_delay_time, 0, -1): time.sleep(1) print(f"終わるまで{i}秒") print("Interactive Pythonコンソールを立ち上げています・・・") soup = BeautifulSoup(driver.page_source, features="lxml") #Define web elements to be tested as dictionary where element ids are the keys. test_element_ids = { "dtFilter": { "tag": "select", "click_el_xpath": "/html/body/div/div[1]/div[2]/div/div/div[1]/div/div/div[1]/div[1]/div/div/div" }, "maxAmount": { "tag": "input", }, "maxSigma": { "tag": "input", }, "pl": { "tag": "select", "click_el_xpath": "/html/body/div/div[1]/div[2]/div/div/div[1]/div/div/div[1]/div[5]/div/div/div" }, "reason": { "tag": "select", "click_el_xpath": "/html/body/div/div[1]/div[2]/div/div/div[1]/div/div/div[1]/div[6]/div/div/div/div[1]" } } for test_el_id in test_element_ids: test_el = test_element_ids[test_el_id] if test_el["tag"] == "select": el = driver.find_element_by_xpath(test_el["click_el_xpath"]) el.click() soup = BeautifulSoup(driver.page_source, features="lxml") select_items = [ tag.text for tag in soup.find( id=test_el_id).find_next_sibling().select("div.option") ] print(f"number of items in select box: {len(select_items)}") for select_item in select_items: click_el = driver.find_element_by_css_selector( f"[data-value='{select_item}']") el.click() click_el.click() time.sleep(5) elif test_el["tag"] == "input": test_round = 1 while test_round < 6: test_input_number = int(random.random() * random.choice([ 10, 100, 1000, 10000, 100000, 1000000, 10000000, 10000000, 100000000 ])) el = driver.find_element_by_id(test_el_id) el.clear() el.click() el.send_keys(test_input_number) time.sleep(5) test_round += 1 el.clear() el.send_keys(0) except Exception as e: print( f"(EXCEPT) An error occurred: {str(e)} Attempting to enter debug mode at point of error." ) embed() finally: print("プログラムが正常終了しました。ウェブドライバーを終了します。お疲れ様でした。") embed() driver.close()
from msedge.selenium_tools import Edge, EdgeOptions from PIL import Image from selenium.webdriver import ActionChains options = EdgeOptions() # options.add_argument("headless") # options.add_argument("disable-gpu") options.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging']) # options.add_argument('-kiosk') #全屏打开 wd = Edge(options = options) wd.get('https://kyfw.12306.cn/otn/resources/login.html') time.sleep(1) #全屏 wd.maximize_window() #点击账户登录 wd.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click() time.sleep(1) #screenshot wd.save_screenshot('./10.screenshot.png') code_img_ele = wd.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[2]/div[3]/div/div[4]/img') #裁剪截图 location = code_img_ele.location print('location:' ,location) size = code_img_ele.size print('size:',size)