Best Python code snippet using SeleniumBase
pvpoke_meta.py
Source:pvpoke_meta.py
...14class Pokemon:15 def __init__(self, name: str, score: str):16 self.name = name17 self.score = score18def get_new_driver(headless: bool = True) -> WebDriver:19 chrome_options = webdriver.ChromeOptions()20 if headless: 21 chrome_options.add_argument('--headless')22 chrome_options.add_argument('--disable-gpu')23 chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])24 25 service = Service('./chromedriver.exe')26 service.start()27 driver = webdriver.Chrome(service=service, options=chrome_options)28 driver.get('https://pvpoketw.com/battle/')29 time.sleep(2)30 wait = WebDriverWait(driver, 2)31 muilti_battle: WebElement = wait.until(EC.element_to_be_clickable(driver.find_element(By.CSS_SELECTOR, "#main > div.section.league-select-container.white > div > a:nth-child(2)")))32 muilti_battle.click()33 34 return driver35def get_score(pokemon: Pokemon) -> Pokemon:36 driver = get_new_driver()37 wait = WebDriverWait(driver, 2)38 try:39 pokemon_selector_el: WebElement = wait.until(EC.element_to_be_clickable(driver.find_element(By.CSS_SELECTOR, "#main > div.section.poke-select-container.multi > div:nth-child(1) > select")))40 pokemon_selector: Select = Select(pokemon_selector_el)41 pokemon_selector.select_by_visible_text(pokemon.name)42 battle_button: WebElement = wait.until(EC.element_to_be_clickable(driver.find_element(By.CSS_SELECTOR, "#main > div.section.battle > button.battle-btn.button")))43 battle_button.click()44 time.sleep(0.5)45 url = driver.current_url46 driver.get(url)47 time.sleep(1.5)48 score_el: WebElement = wait.until(EC.element_to_be_clickable(driver.find_element(By.CSS_SELECTOR, "#main > div.section.battle > div:nth-child(6) > div > div > div > div > div.label.rating.star > span")))49 pokemon.score = score_el.text50 except:51 pass52 driver.quit()53 print(f'{pokemon.name}: {pokemon.score}')54 return pokemon55if __name__ == '__main__':56 driver = get_new_driver()57 wait = WebDriverWait(driver, 2)58 pokemon_selector_el: WebElement = wait.until(EC.element_to_be_clickable(driver.find_element(By.CSS_SELECTOR, "#main > div.section.poke-select-container.multi > div:nth-child(1) > select")))59 pokemon_list: List[str] = pokemon_selector_el.text.split('\n')60 print("Length of pokemon list: {}".format(len(pokemon_list)))61 driver.quit()62 results: List[Pokemon] = []63 with ProcessPoolExecutor(cpu_count() - 1) as executor:64 futures = [executor.submit(get_score, Pokemon(pokemon.strip(), -1)) for pokemon in pokemon_list[1:]]65 for future in as_completed(futures):66 results.append(future.result())67 results.sort(key=lambda x: int(x.score), reverse=True)68 with open(f'pvpoke_meta_{int(time.time())}.csv', 'w') as f:69 f.write('name,score\n')70 for pokemon in results:...
keyword-tool-crapper.py
Source:keyword-tool-crapper.py
...16# base_keyword = 'женÑки обÑвки'17# lang = 'BG:bg'18# driver = get_seciruty_expose_cdp_driver()19time.sleep(5.5)20def get_new_driver():21 # # Server connection22 if str(sys.argv).count('local') > 0:23 # driver = webdriver.Firefox('/var/www/html/seo/SEO_py/')24 newDriver = webdriver.Chrome('/var/www/html/seo/SEO_py/chromedriver') # Optional argument, if not specified will search path.25 newDriver.set_window_size(1920, 1080, newDriver.window_handles[0])26 27 return newDriver28 else:29 newDriver = webdriver.Remote(30 command_executor='http://127.0.0.1:4444/wd/hub', 31 desired_capabilities=DesiredCapabilities.CHROME)32 33 newDriver.set_window_size(1920, 1080, newDriver.window_handles[0])34 35 return newDriver36 37# driver.maximize_window()38def change_language(language, useDriver):39 xpath = f"//option[@value='{language}']"40 optionToSelect = WebDriverWait(useDriver, 15).until(EC.presence_of_element_located((By.XPATH, xpath)))41 optionToSelect.click()42 time.sleep(0.5)43def get_results(language ,search_term, useDriver):44 useDriver.get('https://keywordtool.io/')45 time.sleep(13)46 47 change_language(language, useDriver)48 search_box = useDriver.find_element_by_name('keyword')49 search_box.send_keys(search_term)50 search_box.submit()51 time.sleep(17)52 53 useDriver.get_screenshot_as_file("screenshot1.png")54 55 try:56 searchForSuccessElement = WebDriverWait(useDriver, 25).until(57 EC.presence_of_element_located((By.ID, "branding"))58 )59 except Exception as e:60 useDriver.get_screenshot_as_file("screenshot2.png")61 useDriver.quit()62 63 return get_results(language, search_term, get_new_driver())64 65 print('something went wrong. no #content element presented.')66 # driver.quit()67 time.sleep(5)68 69 useDriver.get_screenshot_as_file("screenshot3.png")70 71 time.sleep(10)72 useDriver.get_screenshot_as_file("screenshot4.png")73 74 time.sleep(3)75 76 text_area_with_all_words = WebDriverWait(useDriver, 45).until(77 EC.presence_of_all_elements_located((By.NAME, "copy_all"))78 )79 80 keywords = ''81 for textarea in text_area_with_all_words:82 text_area_value = textarea.get_attribute('value')83 if len(text_area_value) > 0:84 keywords = text_area_value85 break86 keywords = keywords.replace('\n',',').split(',')87 88 print(json.dumps(keywords))...
aws_scraper.py
Source:aws_scraper.py
...3from bs4 import BeautifulSoup4from selenium import webdriver5from time import sleep6import re7def get_new_driver():8 options = webdriver.ChromeOptions()9 options.binary_location = '/opt/google/chrome/google-chrome'10 options.add_argument('headless')11 driver = webdriver.Chrome('./chromedriver', chrome_options=options)12 return driver13review_grabber = lambda x: re.search('overall:\s\d\.\d*(.*[\s\S]*\d characters)', x).group(1)14def review_cleaner(string):15 x = review_grabber(string)16 x = re.sub('\d* characters', '', x)17 x = re.sub('\n', ' ', x)18 x = re.sub(',', '', x)19 return x20def get_user_reviews_csv(url):21 counter = 022 review_number = 123 for i in range(0,101):24 driver.get(f'https://www.beeradvocate.com{url}?view=beer&sort=&start={i*25}')25 sleep(2)26 beer_page = driver.page_source27 beer_page_soup = BeautifulSoup(beer_page, 'lxml')28 29 reviews = beer_page_soup.find_all('div', {'id':'rating_fullview_content_2'})30 31 counter += 132 print(f'{url} -- page {counter}')33 34 for count, review in enumerate(reviews):35 score = review.find('span', {'class': 'BAscore_norm'}).text36 breakdown = review.find('span', {'class': 'muted'}).text37 u_names = review.find('a', {'class':'username'}).text38 try:39 r_text = review_cleaner(reviews[count].text)40 except:41 r_text = "No Review"42 43 master_list = [str(review_number), url, score, breakdown, u_names, r_text]44 with open('./aws_user_reviews.csv', 'a+') as f:45 print(','.join(master_list), file=f)46 47 review_number += 148 49 sleep(2)50sleep(50)51driver = get_new_driver()52df_beer = pd.read_csv('./df_beer.csv', encoding = "latin1")...
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!!