Thursday, 27 August 2015

Upload file

a = driver.find_element_by_xpath('random_xpath')
a.send_keys("C:\Users\user123\Downloads\myimage.jpg")

back button

driver.execute_script("window.history.go(-1)")

Image found or not

if source code of image is:
<img src="/images/abc.png" />

code:
a=driver.find_element_by_xpath("//img[contains(@src,'/images/abc.png')]") if a.is_displayed(): b="Element found" else: b="Element not found"

Using select box

a.find_element_by_xpath("//select[@name='Food']/option[text()='Veg']").click()

Clearing a field

a = driver.find_element_by_name("phoneno")
a.clear()

Reading attributes of elements

a = driver.find_element_by_name("Password")
b=a.get_attribute('placeholder')

Entering data into elements

a = driver.find_element_by_id("username")
a.send_keys("i love python...n selenium")

Saving screeshot

driver.save_screenshot('test1_1.png')

Reading text of an element

a = driver.find_element_by_xpath("/html/body/div[4]/div/ul/li[1]/a")
b=a.text

Clicking an element

a = driver.find_element_by_xpath("/html/body/div[4]/div/ul/li[1]/a")
a.click()

Different methods to find an element

-By Xpath
a = driver.find_element_by_xpath("/html/body/div[4]/div/ul/li[1]/a")

-By ID
a = driver.find_element_by_id("username")

-By Link text
a = self.find_element_by_link_text("Sign Up!")

-By Name
a = driver.find_element_by_name("email")