How To Use Selenium WebDriver In Python To Make Web Automation Testing Example

Selenium is mainly used for automated testing, it support multiple browsers, it can simulate browser for web page loading and for solving JavaScript rendering problems in crawlers. This example will show you how to use selenium webdriver in Python.

1. Declare Web Browser Object.

# import webdriver class from selenium package.
from selenium import webdriver

#Webdriver can be considered as the driver of the web browser, to semiulate web browser we must use webdriver, webdriver support a variety of browsers, here we use google Chrome as an example.
browser = webdriver.Chrome()

2. Browse Web Page And Get Page Html Source Code.

# import webdriver class.
from selenium import webdriver

# create google chrome browser webdriver.
browser = webdriver.Chrome()

web_page_url = 'https://www.google.com'

# use above webdriver to browse google home page.
browser.get(web_page_url)

# browser.page_source will return all web page html source code.
page_source_html = browser.page_source

# print page source html code.
print(page_source_html)

# close and quit google chrome.
browser.close()

3. Find Single Web Element.

WebDriver provide below methods to locate single web element.

  1. find_element_by_name
  2. find_element_by_id
  3. find_element_by_xpath
  4. find_element_by_link_text
  5. find_element_by_partial_link_text
  6. find_element_by_tag_name
  7. find_element_by_class_name
  8. find_element_by_css_selector
  9. find_element(by_condition, value) : This method can implement all above method. For example,  find_element_by_name can be implemented by find_element(BY.NAME, ‘tom’) etc.
from selenium import webdriver

browser = webdriver.Chrome()

url = 'https://www.taobao.com'

browser.get(url)

# find web element by id.
input_first = browser.find_element_by_id('q')

# find web element by css selector.
input_second = browser.find_element_by_css_selector('#q')

# find web element by xpath
input_third = browser.find_element_by_xpath('//*[@id="q"]')

# print out above web element
print(input_first,input_second,input_third)

# close and quite google chrome web browser
browser.close()

4. Find Multiple Web Elements.

input_first = browser.find_elements_by_id('q')

input_first = browser.find_elements_by_name('tom')

5. Use Selenium WebDriver To Send Keyword & Click Button.

Below code will input keyword in search box and implement search action in google chrome automatically.

# import webdriver and time class.
from selenium import webdriver

import time

# create google chrome web browser.
browser = webdriver.Chrome()

# browse a web page url.
browser.get('https://www.taobao.com')

# find the search input text box web element.
input = browser.find_element_by_id('q')

# send keyword into above search text box.
input.send_keys('iPhone')

# sleep 5 seconds.
time.sleep(5)

# clear the search input text box.
input.clear()

# input another search keyword in search box.
input.send_keys('iPad')

# find the search submit button.
button = browser.find_element_by_class_name('btn-search')

# click above button to search.
button.click()

6. Use Selenium WebDriver To Implement Drag & Drop Action.

# import webdriver class.
from selenium import webdriver

# import ActionChains class.
from selenium.webdriver import ActionChains

# create google chrome web browser.
browser = webdriver.Chrome()

# browse below url in google chrome.
url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'

browser.get(url)

# switch to iframe.
browser.switch_to.frame('iframeResult')

# find the drag & drop source object.
source = browser.find_element_by_css_selector('#draggable')

# find the drag & drop target object.
target = browser.find_element_by_css_selector('#droppable')

# create an ActionChains object.
actions = ActionChains(browser)

# connect drag & drop action source and target object.
actions.drag_and_drop(source, target)

# implement drag & drop action.
actions.perform()#执行动作

7. Run JavaScript In Selenium WebDriver.

Below code will invoke javascript in selenium webdriver to scroll the scroll bar. This kind of action do not supported by selenium webdriver by default, so we need to use javascript to implement it.

 import webdriver class
from selenium import webdriver

# create a google chrome web browser.
browser = webdriver.Chrome()

# browse below url.
browser.get('https://www.zhihu.com/explore')

# execute javascript which will scroll the page to the bottom.
browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')

# execute javascript to popup an alert dialog.
browser.execute_script('alert("To Bottom")')

8. Get Web Element’s Attribute.

Call web_element.get_attribute(attr_name) method.

from selenium import webdriver

browser = webdriver.Chrome()

url = 'https://www.zhihu.com/explore'

browser.get(url)

# get website logo.
logo = browser.find_element_by_id('zh-top-link-logo')

# get logo class attribute value.
logo_attr_class = logo.get_attribute('class')

# print above class attribute value.
print(logo_attr_class)

browser.close()

9. Get Web Element’s Text, Id, Location, Tag Name and Size Value.

Call web_element’s text, id, location, tag_name and size property.

from selenium import webdriver
browser = webdriver.Chrome()

url = 'https://www.zhihu.com/explore'

browser.get(url)

# find input text box
input = browser.find_element_by_class_name('zu-top-add-question')

# get input text box text.
input_text = input.text

# print out the text.
print(input_text)

# print input text box id, location, tag_name and size
print(input.id)

print(input.location)

print(input.tag_name)

print(input.size)

browser.close()

10. Switch Frame In Selenium.

from selenium import webdriver
......
browser = webdriver.Chrome()
......
browser.switch_to.frame('frame_name')
......

11. Implicit Wait.

When an implicit wait is used to execute the selenium test, if WebDriver does not find the element in the DOM, it will continue to wait and throw cannot find the element exception when the specified time timeout.

from selenium import webdriver
browser = webdriver.Chrome()

# wait 10 seconds for loading web page, if not loaded in 10 seconds, it will throw an exception.
browser.implicitly_wait(10)
browser.get('https://www.yahoo.com')

12. Explicit Wait.

Specify a wait condition, and a maximum wait time, the program will determine whether the condition is met in the wait time, if it met, it will return, if it do not met, it will continue to wait, and an exception will be thrown after the wait time timeout.

# import webdriver class.
from selenium import webdriver

# import By class
from selenium.webdriver.common.by import By

# explicit wait need import WebDriverWait class.
from selenium.webdriver.support.ui import WebDriverWait

# the expected_conditions class is used to specify the wait condition.
from selenium.webdriver.support import expected_conditions as EC

# create google chrome web browser and browse a web page.
browser = webdriver.Chrome()
browser.get('https://www.taobao.com/')

# create the WebDriverWait class, the timeout value is 10 seconds.
wait = WebDriverWait(browser, 10)

# wait until to find the input text box which id value is 'q'.
input = wait.until(EC.presence_of_element_located((By.ID, 'q')))

# wait until the button is clickable.
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search')))

print(input, button)

13. Implement Web Browser Forward & Back.

import time
from selenium import webdriver

# create the web browser.
browser = webdriver.Chrome()

# browse three web page url one by one.
browser.get(url_1)
browser.get(url_2)
browser.get(url_3)

# now go back to url_2.
browser.back()

time.sleep(1)

# now forward to url_3
browser.forward()

browser.close()

14. Manage Web Browser Cookie.

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')

# get current web browser cookies and print.
print(browser.get_cookies())

# add a new cookie to web browser.
browser.add_cookie({'name': 'name', 'domain': 'www.zhihu.com', 'value': 'germey'})
print(browser.get_cookies())

# delete all web browser cookies
browser.delete_all_cookies()
print(browser.get_cookies())

15. Open New & Switch Between Browser Windows.

import time
from selenium import webdriver
browser = webdriver.Chrome()

# browse url_1 in current window.
browser.get(url_1)

# open a new window by execute javascript.
browser.execute_script('window.open()')

# print all window handlers.
print(browser.window_handles)

# switch to the newly opened window.
browser.switch_to_window(browser.window_handles[1])

# browse url_2 in new opened window.
browser.get(url_2)

time.sleep(1)

# switch back to the original browser window
browser.switch_to_window(browser.window_handles[0])

# browse url_3 in the original web browser window.
browser.get(url_3)

16. Exception Management.

from selenium import webdriver

# import TimeoutException, NoSuchElementException class.
from selenium.common.exceptions import TimeoutException, NoSuchElementException

browser = webdriver.Chrome()

# below source code will demo TimeoutException.
try:
    browser.get('https://www.google.com')
except TimeoutException:
    print('Time Out')


# below source code will demo NoSuchElementException.
try:
    browser.find_element_by_id('hello')
except NoSuchElementException:
    print('No Element')
    
finally:
    browser.close()

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.