Tuesday, June 23, 2015

Forward and Backward Navigation

Click here to get Java Snippet for this script.

__author__ = 'Gaurav.Khanna'

from selenium import webdriver

# Initializing FireFox Driver
webDriver = webdriver.Firefox()

# Assigning URL to variable 'baseUrl'
baseUrl = "http://book.theautomatedtester.co.uk"

# Open the link
webDriver.get("http://book.theautomatedtester.co.uk")

# Maximize browser window
webDriver.maximize_window()

# Click on link
webDriver.find_element_by_link_text("Chapter1").click()

# Print Page Title
print(" Page Title : " + webDriver.title)

# Using Back Method
webDriver.back()

# Print Page Title
print(" Page Title : " + webDriver.title)

# Using Forward Method
webDriver.forward()

# Print Page Title
print(" Page Title : " + webDriver.title)

# This will close the browser
webDriver.quit()

File Uploading

Click here to get Java Snippet for this script.

__author__ = 'Gaurav.Khanna'

from selenium import  webdriver

# Initializing FireFox Driver
webDriver = webdriver.Firefox()

# Assigning URL to variable 'baseUrl'
baseUrl = "http://www.htmlcodetutorial.com/forms/_INPUT_TYPE_FILE.html"

# Open the link
webDriver.get(baseUrl)

# Maximize browser window
webDriver.maximize_window()

# Upload File
webDriver.find_element_by_name("upfile").send_keys("C:\\gaurav\\gaurav.txt")

# Click on Submit Button
webDriver.find_element_by_xpath("//input[@value='Submit']").click()

#This will close the browser
webDriver.quit()

Extracting Links from Web Page

Click here to get Java Snippet for this script.

__author__ = 'Gaurav.Khanna'

from selenium import webdriver

# Initializing FireFox Driver
webDriver = webdriver.Firefox()

# Open URL
webDriver.get("http://not-just-a-tester.blogspot.in/")

# Find all anchor tags
allLinks = webDriver.find_elements_by_tag_name("a")

# Print All Links
for links in allLinks:
    print(links.get_attribute('href'))
    print("\n")

# Close Browser
webDriver.close()

Basic Script 2

Click here to get Java Snippet for this script.

__author__ = 'Gaurav.Khanna'

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import select

# Initializing FireFox Driver
webDriver = webdriver.Firefox()

# Assigning URL to variable 'baseUrl'
baseUrl = "http://www.webmath.com/"

# Wait for 30 seconds using Implicit Wait
WebDriverWait(webDriver,30)

# Open the link
webDriver.get(baseUrl)

# Click on element
webDriver.find_element_by_id("www-content-wrap").click()

# Select `Simple Interest` option from drop down
select.Select(webDriver.find_element_by_id("topicItem")).select_by_visible_text("Interest, Simple")

# Click on element
webDriver.find_element_by_css_selector("option[value=\"simpinterest.html\"]").click()

# Clear Field
webDriver.find_element_by_name("principal").clear()

# Enter value
webDriver.find_element_by_name("principal").sendKeys("1000")

# Clear Field
webDriver.find_element_by_name("interest").clear()

# Enter value
webDriver.find_element_by_name("interest").send_keys("1")

# Clear Field
webDriver.find_element_by_name("desired_time").clear()

# Enter value
webDriver.find_element_by_name("desired_time").send_keys("5")

# Click on element
webDriver.find_element_by_name("//input[@value='Find the amount of interest']").click()

# Close Browser
webDriver.close()

Get Attribute Method

Click here to get Java Snippet for this script.

__author__ = 'Gaurav.Khanna'

from selenium import webdriver

# Initializing FireFox Driver
webDriver = webdriver.Firefox()

# Assigning URL to variable 'baseUrl'
baseUrl = "http://book.theautomatedtester.co.uk/chapter1"

# Open the link
webDriver.get(baseUrl)

# Locate Button and Store the value of button to 'attributeValue' variable
attributeValue = webDriver.find_element_by_id("verifybutton").get_attribute("value")

# Printing value of 'attributeValue'
print("Attribute Value : " + attributeValue)

# This will close the browser
webDriver.quit()