Some sample codes for using selenium in Python just for fun.
- Google Search
- GitHub Search
- GitHub SignIn
- Yahoo Maktoob News
- Yahoo Search
- Gmail Register (Need captcha...)
- GitHub Register (Need captcha...)
from selenium import webdriver
- webdriver.Firefox
- webdriver.FirefoxProfile
- webdriver.Chrome
- webdriver.ChromeOptions
- webdriver.Ie
- webdriver.Opera
- webdriver.PhantomJS
- webdriver.Remote
- webdriver.DesiredCapabilities
- webdriver.ActionChains
- webdriver.TouchActions
- webdriver.Proxy
https://selenium-python.readthedocs.io/api.html
There are various strategies to locate elements in a page. You can use the most appropriate one for your case.
- find_element_by_id
- find_element_by_name
- find_element_by_xpath
- find_element_by_link_text
- find_element_by_partial_link_text
- find_element_by_tag_name
- find_element_by_class_name
- find_element_by_css_selector
- find_elements_by_name
- find_elements_by_xpath
- find_elements_by_link_text
- find_elements_by_partial_link_text
- find_elements_by_tag_name
- find_elements_by_class_name
- find_elements_by_css_selector
Apart from the public methods given above, there are two private methods which might be useful with locators in page objects. These are the two private methods: find_element and find_elements.
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
login_form = driver.find_element_by_id('loginForm')
heading1 = driver.find_element_by_tag_name('h1')
continue = driver.find_element_by_name('continue')
username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
content = driver.find_element_by_class_name('content')
content = driver.find_element_by_css_selector('p.content')
continue_link = driver.find_element_by_link_text('Continue')
continue_link = driver.find_element_by_partial_link_text('Conti')
login_form = driver.find_element_by_xpath("/html/body/form[1]")
login_form = driver.find_element_by_xpath("//form[1]")
login_form = driver.find_element_by_xpath("//form[@id='loginForm']")
username = driver.find_element_by_xpath("//form[input/@name='username']")
username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
username = driver.find_element_by_xpath("//input[@name='username']")
clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")
clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")
If you have installed Selenium Python bindings, you can start using it from Python like this.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
https://chromedriver.chromium.org/getting-started
https://selenium-python.readthedocs.io/getting-started.html
My nickname is Max, Programming language developer, Full-stack programmer. I love computer scientists, researchers, and compilers.
A team includes some programmer, developer, designer, researcher(s) especially Max Base.