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("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.
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)|
Element |
|
|
|
|
|
|
|
|
|
|
|
Throws exception NoSuchElementException if there are no elements matching the locators |
|
driver.find_element_by_css_selector(".RNNXgb input").click()Click_element = driver.find_element_by_css_selector(".RNNXgb input")
Click_element.click()Click_elements = driver.fimd_elements_by_css_selector(".RNNXgb input")Click_element = driver.find_element_by_css_selector(".RNNXgb input")
Click_element.send_keys('hello')close() method close only the active web page in the 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()






Very nice
ReplyDeleteSuperb
ReplyDeleteVery useful and anyone can learn easily.
ReplyDeleteThanks BhagavathiMuthukrishnan
DeleteSuperb
ReplyDeleteGreat job Joyal...keep going
ReplyDeleteNice one Joel 😊👌
ReplyDeleteThanks Joanse
DeleteGreat work!
Delete