Selenium is a powerful library of commands in multiple languages (C#, Haskell, Java, JavaScript, Objective-C, Perl, PHP, Python, R, and Ruby) that allow a programmer to automate browser interaction. This is incredibly useful for developers testing applications.
Selenium offers methods to:
Using these methods, a developer can have automatic tests checking:
Selenium runs in webdrivers, which are similar to a normal web browser but allow Selenium to interact with them. A Selenium test typically opens up a new driver instance of whatever browser the developer is testing in, which is always a clean slate. This way, when running a Selenium test, the developer does not have to worry about previous cookies, or a browser cache affecting the results of their application.
Selenium also works when running a webdriver in headless mode.
This is a very basic example or starting Selenium, accessing and using a page and then shutting down Selenium within NUnit.
What is Selenium?
Selenium is a library of commands to help a programmer interface with a browser like a real user.
Things that Selenium does:
Finding element(s) in a webpage's html
Finds a single element:
driver.find_element_by_css_selector("css.selector.of.element")
CSS Selector helpdriver.find_element_by_xpath("//xpath//of//element")
XPATH helpdriver.find_element_by_name("name_of_element")
driver.find_element_by_id("id_of_element")
driver.find_element_by_partial_link_text("element_link_text")
driver.find_element_by_class_name("class_name_of_element")
driver.find_element_by_tag_name("tag_name_of_element")
Finds a list of elements:
driver.find_elements_by_css_selector("css.selector.of.elements")
driver.find_elements_by_xpath("//xpath//of//elements")
driver.find_elements_by_name("name_of_elements")
driver.find_elements_by_partial_link_text("elements_link_text")
driver.find_elements_by_class_name("class_name_of_elements")
driver.find_elements_by_tag_name("tag_name_of_elements")
Official documentation: selenium-python read the docs
Interact with elements:
"method" represents any of the above methods to find an element or list of elements.
click function:
driver.find_element_by_method.click()
send_keys function:
driver.find_element_by_method.send_keys("text")
sends the String "text" to the element found.driver.find_element_by_method.send_keys(KeyCode.UP)
sends the KeyCode for the up arrow key to the element found.