Selenium in Python
Selenium basically is web-automation library in Python. Maybe, it is the best known packages of Python. That’s why, Selenium was written as Java, C# in the same time as well. You can test your built web-page, get some information etc. In this, I’m going to explain how to use selenium with some code lines. Before that, you have to download selenium on your computer as like:
pip install selenium
and found webdriver according to yourweb browser. For instance, I’m using Chrome and I downloaded chrome webdriver. You can reach webdrivers from these links:
To Chrome: https://sites.google.com/chromium.org/driver/
To Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
To Firefox: https://github.com/mozilla/geckodriver/releases
Note: Be attention about appropriate version and downloading zip file !
Also, the link that you can read its documents:
Apart from this, I recommend that you look at my github repository. Its link:
You can reach that you want page with this block of code basically.
from selenium import webdriverfull_path = "" # full path that you use web-driver.driver = webdriver.Chrome(full_path)url = "http://github.com/CanGulmez"driver.get(url) # gets web page.
And some function that you can use it.
from selenium import webdriver
import timefull_path = "" # full path that you use web-driver.driver = webdriver.Chrome(full_path)url = "http://github.com/CanGulmez"driver.get(url) # gets web page.
driver.maximize_windows() # expands the screen.
driver.save_screenshot("github.png") # saves screenshot.
time.sleep(2) # waits for 2 seconds.
print(driver.title) # prints page'title.
time_sleep(2) # waits for 2 seconds.url_2 = "http://github.com" # the new urldriver.get(url_2) # gets new web page.
time.sleep(2) # waits for 2 seconds.
driver.back() # returns first page.
time.sleep(2) # waits for 2 seconds.if "CanGulmez" in driver.title:
driver.save_screenshot("github-CanGulmez.png")driver.close() # closes the page.
Basically, you can see most used functions in here. But, Selenium is able to more than. There are various strategies to locate elements in a page. You can use the most appropriate one for your case. Selenium provides the following methods to locate elements in a page:
- 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
To find multiple elements (these methods will return a list):
- 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
Locating by Id:
When you know the id attribute of an element that you should this method. For inistance:
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
</form>
</body>
</html>
The form element can be located like this:
driver.find_element_by_id('loginForm')
To more detail that you can visit:
https://selenium-python.readthedocs.io/locating-elements.html