Showing posts with label Selenium Webdriver in Python. Show all posts
Showing posts with label Selenium Webdriver in Python. Show all posts

Tuesday, 18 March 2014

Synchronization in selenium webdriver in python - Example

Synchronization makes selenium to wait for specific elements before performing operation on them. We can use below synchronization methods in selenium. 

1. Page Load Synchrnoization – Implicit Wait

We can set the default page navigation timeout. Below statement will set the navigation timeout as 50. This means that selenium script will wait for maximum 50 seconds for page to load.  If page does not load within 50 seconds, it will throw an exception.
driver.set_page_load_timeout(50)

2. Element Synchronization – Implicit Wait

We can set the default element existance timeout. Below statement will set the default object synchronization timeout as 20. This means that selenium script will wait for maximum 20 seconds for element to exist.  If Web element does not exist within 20 seconds, it will throw an exception.
driver.implicitly_wait(20)

3. Synchronization based upon specific condition – Explicit Waits

We can also insert custom synchronization points in the script using WebDriverWait class. Please remember that you have to import this class before you use it.
from selenium.webdriver.support import expected_conditions as EC

We can also instruct selenium to wait until element is in expected condition.
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)

element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))


from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

# available since 2.4.0

from selenium.webdriver.support import expected_conditions as EC

# available since 2.26.0

ff = webdriver.Firefox()

ff.get("http://somedomain/url_that_delays_loading")

try:

    element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))

finally:

    ff.quit()





What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Get the selected value in combobox in selenium webdriver in python.

We can get the data from combobox using get_attribute method as illustrated in the below example.

from selenium import webdriver

from selenium.webdriver.support.ui import Select


driver = webdriver.Firefox()

driver.maximize_window()

driver.get("http://register.rediff.com/register/register.php")

try:

   
 e = driver.find_element_by_name("DOB_Day")

 select = Select(e)

 select.select_by_index(3)

 selOption = select.first_selected_option
   
 print(selOption.get_attribute("value"))
   
 driver.close()

except Exception as e:

    print ("Exception occured", format(e));

finally:

    driver.quit()

    print ("finally")




What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Reading data from the webpage in selenium webdriver in python - Example

Selenium API provides 2 important methods to read data from web elements.
  1. value_of_css_property – gets the value of css property of the element
  2. get_attribute – gets the value of given attribute.
We can also get the inner text of element using text property.

We can also check if
  1. Element is displayed using is_displayed mehtod
  2. Element is selected using is_selected method
  3. Element is enabled using is_enabled method
x = e.value_of_css_property("width");
x = e.get_attribute("onfocus");

x = e.is_selected();
x = e.is_displayed();
x = e.isEnabled();

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Working with radiobuttons in Selenium Webdriver in Python - Example

We can select the radiobutton using click method as illustrated in the below example in Python.

from selenium import webdriver

from selenium.webdriver.support.ui import Select


driver = webdriver.Firefox()

driver.maximize_window()

driver.get("http://register.rediff.com/register/register.php")

try:
 driver.find_element_by_xpath("//*[@id='wrapper']/table[2]/tbody/tr[21]/td[3]/input[2]").click()
   
    driver.close()

except Exception as e:

    print ("Exception occured", format(e));

finally:

    driver.quit()

    print ("finally")




What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Working with Checkboxes in Selenium Webdriver in Python - Example

We can first see if the checkbox is selected using is_selected() method. Then using click method we can perform the operations such as selecting or deselecting the checkboxes as illustrated in the below example.


from selenium import webdriver

from selenium.webdriver.support.ui import Select


driver = webdriver.Firefox()

driver.maximize_window()

driver.get("http://register.rediff.com/register/register.php")

try:
   
elem= driver.find_element_by_name("chk_altemail")
  
   if (elem.is_selected()):

       print("Checkbox is selected..now deselecting")

      elem.click()

   else:

       print("Checkbox is not selected..now selecting it")

      elem.click()
   
   driver.close()

except Exception as e:

    print ("Exception occured", format(e));

finally:

    driver.quit()

    print ("finally")



What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

How to click on the buttons in selenium webdriver in Python?

We can click on the buttons using click() method as illustrated in the below example.


from selenium import webdriver

from selenium.webdriver.support.ui import Select


driver = webdriver.Firefox()

driver.maximize_window()

driver.get("http://www.amazon.in")

try:
    driver.find_element_by_xpath("//*[@id='twotabsearchtextbox']").send_keys("Selenium")

    driver.find_element_by_xpath("//*[@id='nav-bar-inner']").click()
   
    driver.close()

except Exception as e:

    print ("Exception occured", format(e));

finally:

    driver.quit()

    print ("finally")


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Selecting value from the combobox in selenium webdriver in python - Example

To work with drop down elements you will need to import below class.
from selenium.webdriver.support.ui import Select

We can select the value from the dropdown using 3 different methods.
1.      select_by_visible_text – using the actual text displayed in drop down.
2.      select_by_value –using the value of the option
3.      select_by_index – using position of the item

Below example demonstrates how we can select the value from the drop down using select_by_visible_text method.

from selenium import webdriver

from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()

driver.get("http://register.rediff.com/register/register.php")

driver.maximize_window()

try:
                select=Select(driver.find_element_by_name('country'))
    select.select_by_visible_text("China")

driver.close()

except Exception as e:

    print ("Exception occured", format(e));

finally:

    driver.quit()

    print ("finally")



What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Buy Best Selenium Books

Contributors