Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd6dd1d

Browse files
committed
Selenium Examples
1 parent f8f1c94 commit cd6dd1d

File tree

2 files changed

+175
-4
lines changed

2 files changed

+175
-4
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Python Code
22
Python Code
33

4-
Python Code is a simple collection of various [Python](https://www.python.org/) Code snippets. The
5-
repository breaks up the code into individual .py files, and the full tutorial for each collection of
6-
code is available below. 👍
4+
Python Code is a simple collection of various [Python](https://www.python.org/) Code snippets that are
5+
great for getting started with Python Programming. The repository breaks up the code into individual .py
6+
files, and the full tutorial for each collection of code is available below. 👍
77

88
1. [How To Name And Use Python Variables](https://vegibit.com/how-to-name-and-use-python-variables/)
99
2. [How To Use Python Strings](https://vegibit.com/how-to-use-python-strings/)
@@ -26,4 +26,5 @@ repository breaks up the code into individual .py files, and the full tutorial f
2626
19. [Python Requests](https://vegibit.com/python-requests-library/)
2727
20. [Json In Python](https://vegibit.com/python-json-tutorial/)
2828
21. [XML Parsing](https://vegibit.com/python-xml-parsing/)
29-
22. [Beautiful Soup Web Scraping](https://vegibit.com/python-web-scraping-with-beautiful-soup/)
29+
22. [Beautiful Soup Web Scraping](https://vegibit.com/python-web-scraping-with-beautiful-soup/)
30+
23. [Selenium Python Tutorial](https://vegibit.com/selenium-python-tutorial/)

selenium.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Launching a Selenium-Controlled Browser
2+
from selenium import webdriver
3+
4+
browser = webdriver.Firefox(executable_path=r'C:\python\vrequests\geckodriver.exe')
5+
6+
7+
# Launching Chrome With Selenium
8+
from selenium import webdriver
9+
10+
browser = webdriver.Chrome(executable_path=r'C:\python\vrequests\chromedriver.exe')
11+
12+
13+
# Closing the browser
14+
from selenium import webdriver
15+
16+
browser = webdriver.Firefox(executable_path=r'C:\python\vrequests\geckodriver.exe')
17+
18+
browser.quit()
19+
20+
21+
# Headless Browsing In Chrome or Firefox
22+
from selenium import webdriver
23+
from selenium.webdriver.firefox.options import Options
24+
25+
options = Options()
26+
options.headless = True
27+
browser = webdriver.Firefox(options=options, executable_path=r'C:\python\vrequests\geckodriver.exe')
28+
29+
browser.quit()
30+
31+
# Chrome
32+
from selenium import webdriver
33+
from selenium.webdriver.chrome.options import Options
34+
35+
options = Options()
36+
options.headless = True
37+
browser = webdriver.Chrome(options=options, executable_path=r'C:\python\vrequests\chromedriver.exe')
38+
39+
browser.quit()
40+
41+
42+
# Fetching Specific Pages
43+
from selenium import webdriver
44+
45+
browser = webdriver.Firefox(executable_path=r'C:\python\vrequests\geckodriver.exe')
46+
browser.get('https://duckduckgo.com/')
47+
48+
49+
# Finding Elements On The Page
50+
from selenium import webdriver
51+
52+
browser = webdriver.Firefox(executable_path=r'C:\python\vrequests\geckodriver.exe')
53+
browser.get('https://duckduckgo.com/')
54+
element = browser.find_element_by_xpath('//*[@id="search_form_input_homepage"]')
55+
print(element)
56+
57+
58+
# Typing Into A Text Input
59+
from selenium import webdriver
60+
61+
browser = webdriver.Firefox(executable_path=r'C:\python\vrequests\geckodriver.exe')
62+
browser.get('https://duckduckgo.com/')
63+
element = browser.find_element_by_xpath('//*[@id="search_form_input_homepage"]')
64+
element.send_keys('How do you automate a web browser?')
65+
66+
67+
# How To Press Enter Key
68+
from selenium import webdriver
69+
from selenium.webdriver.common.keys import Keys
70+
71+
browser = webdriver.Firefox(executable_path=r'C:\python\vrequests\geckodriver.exe')
72+
browser.get('https://duckduckgo.com/')
73+
element = browser.find_element_by_xpath('//*[@id="search_form_input_homepage"]')
74+
element.send_keys('How do you automate a web browser?')
75+
element.send_keys(Keys.RETURN)
76+
77+
78+
# Selenium Easy Practice Examples
79+
from selenium import webdriver
80+
from selenium.webdriver.common.keys import Keys
81+
82+
browser = webdriver.Firefox(executable_path=r'C:\python\vrequests\geckodriver.exe')
83+
browser.get('https://www.seleniumeasy.com/test/basic-first-form-demo.html')
84+
85+
input_element = browser.find_element_by_id('user-message')
86+
input_element.send_keys('Check out this great message!')
87+
88+
show_message_button = browser.find_element_by_xpath('//*[@id="get-input"]/button')
89+
show_message_button.click()
90+
91+
92+
# Two Inputs And A Buton Click
93+
from selenium import webdriver
94+
from selenium.webdriver.common.keys import Keys
95+
from selenium.webdriver.firefox.options import Options
96+
97+
options = Options()
98+
options.headless = True
99+
browser = webdriver.Firefox(options=options, executable_path=r'C:\python\vrequests\geckodriver.exe')
100+
browser.get('https://www.seleniumeasy.com/test/basic-first-form-demo.html')
101+
102+
input_element_one = browser.find_element_by_id('sum1')
103+
input_element_one.send_keys('10')
104+
105+
input_element_two = browser.find_element_by_id('sum2')
106+
input_element_two.send_keys('7')
107+
108+
get_total_element = browser.find_element_by_xpath('//*[@id="gettotal"]/button')
109+
get_total_element.click()
110+
111+
result_element = browser.find_element_by_id('displayvalue')
112+
print(result_element.text)
113+
114+
115+
# Drag And Drop With ActionChains
116+
from selenium import webdriver
117+
from selenium.webdriver.common.keys import Keys
118+
from selenium.webdriver.common.action_chains import ActionChains
119+
120+
browser = webdriver.Firefox(executable_path=r'C:\python\vrequests\geckodriver.exe')
121+
browser.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')
122+
123+
source_element = browser.find_element_by_xpath('//*[@id="box7"]')
124+
destination_element = browser.find_element_by_xpath('//*[@id="box107"]')
125+
actions = ActionChains(browser)
126+
actions.drag_and_drop(source_element, destination_element).perform()
127+
128+
129+
# Example Application: Stock Quote Checker
130+
import time
131+
from selenium import webdriver
132+
from selenium.webdriver.common.keys import Keys
133+
134+
browser = webdriver.Firefox(executable_path=r'C:\python\vrequests\geckodriver.exe')
135+
browser.get('https://finance.yahoo.com')
136+
137+
ticker_to_lookup = True
138+
while (ticker_to_lookup != "q"):
139+
ticker_to_lookup = input('What ticker to you want to look up? (q to quit) ')
140+
141+
if ticker_to_lookup == 'q':
142+
browser.quit()
143+
break
144+
145+
quote_lookup_text_input = browser.find_element_by_xpath(
146+
'//*[@id="Col2-0-SymbolLookup-Proxy"]/div/div/div/fieldset/input')
147+
quote_lookup_text_input.send_keys(ticker_to_lookup, Keys.RETURN)
148+
time.sleep(10)
149+
150+
quote_span = browser.find_element_by_xpath(
151+
'/html/body/div[1]/div/div/div[1]/div/div[2]/div/div/div[4]/div/div[3]/div[1]/div/span[1]')
152+
print(ticker_to_lookup + ' is currently ' + quote_span.text)
153+
browser.back()
154+
time.sleep(5)
155+
156+
157+
# An Example Of Using A Wait Function
158+
from selenium import webdriver
159+
from selenium.webdriver.common.by import By
160+
from selenium.webdriver.support.ui import WebDriverWait
161+
from selenium.webdriver.support import expected_conditions as EC
162+
163+
browser = webdriver.Firefox(executable_path=r'C:\python\vrequests\geckodriver.exe')
164+
browser.get('https://www.google.com/earth/')
165+
wait = WebDriverWait(browser, 10)
166+
launchButton = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/header/div/nav[1]/ul[2]/li[2]/a')))
167+
launchButton.click()
168+
169+
170+

0 commit comments

Comments
 (0)