Learn Selenium Webdriver Python 10 most basic things

Selenium Webdriver Python              


1. How to invoke a browser in  selenium ? 

from selenium import webdriver

driver = webdriver.Chrome(path of the chromedriver located) 

from selenium import webdriver

driver = webdriver.Chrome(r'F:\chromedriver.exe')


similarly for other browser:

#driver = webdriver.firefox(path of the firefox driver located) 

#driver = webdriver.ie(path of the ie driver were its located) 

Note: To invoke a specific browser we need that specific browser driver


2. How to invoke the url of the web application to be automated in the browser ?

driver.get(Url of the web application)

driver.get("https://www.google.com/")



3. How to find and locate the specific web element on the webpage (like button, icon, etc) ?

To find a specific web element:

    i) Right click on the web page and select inspect 

    ii) Click on the inspect icon and select the element.





    iii) After finding the web element we have to locate it in the search box of the DOM.

                            

    iv) Using one of the 8 different ways of locators

                1. ID

                2. Name

                3. Class Name

                4.  Link Text

                5. Partial Link Text 

                6. Tag Name 

                7. XPATH Selector

                8. CSS Selector

To know more about locators click here. In selenium we find the element as follow:

For finding a single element we use the below methods

driver.find_element_by_css_selector(css selector)
driver.find_element_by_xpath(xpath selector)
driver.find_element_by_class_name(classname)
driver.find_element_by_name(name)
driver.find_element_by_id(id)
driver.find_element_by_tag_name(tagname)
driver.find_element_by_link_text(link_text)
driver.find_element_by_partial_link_text(partial_link_text)

For finding the list of elements we use the below methods

driver.find_elements_by_css_selector(css selector)
driver.find_elements_by_xpath(xpath selector)
driver.find_elements_by_class_name(classname)
driver.find_elements_by_name(name)
driver.find_elements_by_id(id)
driver.find_elements_by_tag_name(tagname)
driver.find_elements_by_link_text(link_text)
driver.find_elements_by_partial_link_text(partial_link_text)


4. Difference between element and elements ?

 Element

 Elements

 It finds only one web element

 It finds list of web elements/element i.e ->[web elements]

 Even there are multiple elements with same locator it returns only the first most element

 Even there is single element with a locator it returns that web element in a list

                                     -

 As it return elements/element in a list, elements can be accessed using list index 

  Throws exception NoSuchElementException if there are no elements matching the locators

 Returns an empty list if there are no web elements matching the locators


5. How to click the web element using selenium web driver ?

     i) For clicking a element we follow as below:
driver.find_element_by_css_selector(".RNNXgb input").click()
or 
Click_element = driver.find_element_by_css_selector(".RNNXgb input")
Click_element.click()

    ii) For clicking a element in the list of elements we follow as below:
Click_elements = driver.fimd_elements_by_css_selector(".RNNXgb input")
After finding the list of elements we have to access the element using index and then click it
            
                         Click_element[index].click()

Click_elements[0].click()
 



6. How to type the text in web element and get the text of web element using selenium webdriver?

    i ) Typing the text in web element:
Click_element = driver.find_element_by_css_selector(".RNNXgb input")
Click_element.send_keys('hello')


    ii ) Getting the highlighted text of the web element :
                


7. How to get the property of web element like text font color and background color etc ?

Lets use the google page sign button for this 

    i ) To find the background color of the element:

    ii) To find the text font color of the element:




8. How to get the title and url of the web page ?

        i) To get the url of the web page:

        ii)To get the title of the web page:
                

9. 
 How to maximize and minimize the browser window ?

        i) To maximize the window:
                driver.maximize_window()

        ii) To minimize the window:
                driver.minimize_window()

10. Difference between quit and close in selenium webdriver ?

 Close webpage

 Quit webpage

 driver.close()

 driver.quit()

 close() method close only the active web page in the browser

 quit() method close the entire browser


from selenium import webdriver

driver = webdriver.Chrome(r'F:\chromedriver.exe')

driver.get("https://www.google.com/")
driver.maximize_window()

Click_element = driver.find_element_by_css_selector(".RNNXgb input")
Click_element.click()

get_ele_text = driver.find_element_by_css_selector(".ctr-p .gb_g").text
print(get_ele_text)

background_color = driver.find_element_by_css_selector(".gb_rg .gb_4").value_of_css_property('background')
print(background_color)
font_color = driver.find_element_by_css_selector(".gb_rg .gb_4").value_of_css_property('color')
print(font_color)

url = driver.current_url
print(url)
webpage_title = driver.title
print(webpage_title)
driver.quit()

Comments

Post a Comment

Popular posts from this blog

Switch frame in selenium python

Learn basics of python collection data type (list, tuple, dictionary and set) instantly

Python property method