You can get Selenium from here.
from selenium import webdriver
You can get ChromeDriver from here.
chromedriver = "C:/tests/chromedriver.exe"
driver = webdriver.Chrome(executable_path = chromedriver)
You can get GeckoDriver from here.
geckodriver = "C:/tests/geckodriver.exe"
driver = webdriver.Firefox(executable_path = geckodriver)
You can get IEDriverServer from here.
iedriver = "C:/tests/IEDriverServer.exe"
driver = webdriver.Firefox(executable_path = iedriver)
Nothing to download. The SafariDriver is integrated in Safari.
driver = webdriver.Safari()
the_url = "https://anhtester.com"
driver.get(the_url)
Let's try to find this element:
<a href="/sign-up" id="register" name="register" class="cta nav-link">
Sign Up
</a>
the_id = 'register'
element = driver.find_element_by_id(the_id)
the_name = 'register'
element = driver.find_element_by_id(the_name)
the_class_name = 'nav-link'
element = driver.find_element_by_class_name(the_class_name)
the_tag_name = 'a'
element = driver.find_element_by_tag_name(the_tag_name)
Works only for anchor elements.
the_link_text = 'Sign Up'
element = driver.find_element_by_link_text(the_link_text)
Works only for anchor elements.
the_partial_link_text = 'Sign'
element = driver.find_element_by_partial_link_text(the_partial_link_text)
You can extract the CSS Selector from the browser.
Or you can write your own by using an attribute from the element:
*[attribute="attribute_value"]
For our element, a custom CSS Selector would be:
a[href="/sign-up"]
the_css_selector = 'a[href="/sign-up"]'
element = driver.find_element_by_css_selector(the_css_selector)
You can extract the XPath from the browser.
Or you can write your own by using an attribute from the element:
//*[@attribute = "attribute_value"]
For our element, a custom XPath would be:
//a[@href = "/sign-up"]
You can read more about that here.
the_xpath = '//a[@href = "/sign-up"]'
element = driver.find_element_by_xpath(the_xpath)
the_id = 'register'
element = driver.find_element_by_id(the_id)
element.click()
Works only for inputs and text areas.
the_id = 'email'
the_email = 'klaus@werner.de'
element = driver.find_element_by_id(the_id)
element.send_keys(the_email)
Works only for select elements.
<select id="country">
<option value="US">United States</option>
<option value="CA">Canada</option>
<option value="MX">Mexico</option>
</select>
Let's select Canada. 🇨🇦
the_id = 'country'
element = driver.find_element_by_id(the_id)
select_element = Select(element)
select_element.select_by_visible_text('Canada')
the_id = 'country'
element = driver.find_element_by_id(the_id)
select_element = Select(element)
select_element.select_by_value('CA')
the_id = 'country'
element = driver.find_element_by_id(the_id)
select_element = Select(element)
select_element.select_by_index(1)
the_path = 'C:/tests/screenshots/1.png'
driver.save_screenshot(the_path)
This works by using the send_keys method to write the local path of the file in the input type="file" element.
Let's use this example:
<input type="file" multiple="" id="upload_button">
the_file_path = 'C:/tests/files/example.pdf'
the_id = 'upload_button'
element = driver.find_element_by_id(the_id)
element.send_keys(the_file_path)
You can read more about uploading files in a test here.
In some cases, you might need to execute some JavaScript code.
This works exactly like you would execute it in your browser console.
js_code = 'document.getElementById("pop-up").remove()'
driver = execute_script(js_code)
<iframe id="payment_section">
<input id="card_number">
<input id="card_name">
<input id="expiration_date">
<input id="cvv">
</iframe>
the_iframe_id = 'payment_section'
the_element_id = 'card_number'
the_iframe = driver.find_element_by_id(the_iframe_id)
driver.switch_to.frame(the_iframe)
element = driver.find_element_by_id(the_element_id)
element.send_keys('41111111111111')
driver.switch_to.default_content()
You have to store the handle of your current tab in a global variable.
If you have only one tab open, the handle is 0.
global nextTab
global currentTab
nextTab = currentTab + 1
driver.switch_to_window(driver.window_handles[nextTab])
currentTab = currentTab + 1
global previousTab
global currentTab
previousTab = currentTab - 1
driver.switch_to_window(driver.window_handles[previousTab])
currentTab = currentTab - 1
driver.close()
driver.switch_to.alert.accept()
driver.refresh()
the_id = "register"
the_element = driver.find_element_by_id(the_id)
hover = ActionChains(driver).move_to_element(the_element)
hover.perform()
the_id = "register"
the_element = driver.find_element_by_id(the_id)
right_click = ActionChains(driver).context_click(the_element)
right_click.perform()
In order to precisely click on a certain position in a canvas element, you have to provide the offset.
The offset represents the number of pixels to the right and down, starting from the top left corner of your canvas element.
the_id = "register"
the_element = driver.find_element_by_id(the_id)
x = 30
y = 20
offset = ActionChains(driver).move_to_element_with_offset(the_element,x,y)
offset.click()
offset.perform()
the_id = 'register'
element = driver.find_element_by_id(the_id)
element.send_keys(Keys.RETURN)
element_to_drag_id = 'ball'
target_element_id = 'goal'
element_to_drag = driver.find_element_by_id(element_to_drag_id)
target_element = driver.find_element_by_id(target_element_id)
ActionChains(driver).drag_and_drop(element_to_drag_id, target_element).perform()
the_page_source = driver.page_source
cookies_list = driver.get_cookies()
cookie_item = 'shopping_cart'
# delete one cookie
driver.delete_cookie(cookie_item)
# delete all cookies
driver.delete_all_cookies()
the_id = 'register'
list_of_elements = driver.find_elements_by_id(the_id)
first_element = list_of_elements[0]
driver.set_page_load_timeout(20)
from selenium.webdriver.support.ui import WebDriverWait
the_id = 'register'
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, the_id)))
driver.set_window_size(1600, 1200)
the_user_agent = 'hello'
chromedriver = 'C:/tests/chromedriver.exe'
options = webdriver.ChromeOptions()
options.add_argument('--user-agent = '+ the_user_agent)
driver = webdriver.Chrome(
executable_path = chromedriver,
chrome_options = options)
chromedriver = 'C:/tests/chromedriver.exe'
options = webdriver.ChromeOptions()
options.add_argument("--use-fake-ui-for-media-stream")
options.add_argument("--use-fake-device-for-media-stream")
driver = webdriver.Chrome(
executable_path = chromedriver,
chrome_options = options)
chromedriver = 'C:/tests/chromedriver.exe'
extension_path = 'C:/tests/my_extension.zip'
options = webdriver.ChromeOptions()
options.add_extension(extension_path)
driver = webdriver.Chrome(
executable_path = chromedriver,
chrome_options = options)
google_pixel_3_xl_user_agent = 'Mozilla/5.0 (Linux; Android 9.0; Pixel 3 XL Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.98 Mobile Safari/537.36'
pixel_3_xl_emulation = {
"deviceMetrics": {
"width": 411,
"height": 731,
"pixelRatio": 3
},
"userAgent": google_pixel_3_xl_user_agent
}
options = webdriver.ChromeOptions()
options.add_experimental_option("mobileEmulation", pixel_3_xl_emulation)
driver = webdriver.Chrome(
executable_path = chromedriver,
chrome_options = options)
#Get Browser Name
print(browser.name)
#Get Title
print(browser.title)
#Get Current URL
print(browser.current_url)
#Get Current Window Handle
print(browser.current_window_handle)
#Get All Window Handles
handles_list=browser.window_handles
#Get Page Source
print(browser.page_source)
browser.maximize_window()
browser.minimize_window()
browser.switch_to.active_element
browser.switch_to.alert
browser.switch_to.default_content()
# You can pass Window Name or Handle to switch between windows
browser.switch_to.window("window_name")
#You can switch to frame using Name, ID, Index & WebElement
browser.switch_to.frame(1)
browser.switch_to.parent_frame()
browser.back()
browser.forward()
browser.refresh()
#Get all cookies in a list
cookies_list = browser.get_cookies
#Get a Cookie value
cookie_value = browser.get_cookie("my_cookie")
#Delete a Cookie
browser.delete_cookie("my_cookie")
#Delete all Cookies
browser.delete_all_cookies()
#Add Cookie
browser.add_cookie({"name:value"})
#Find Element(s) By ID
element = browser.find_element_by_id("txt_1")
elements = browser.find_elements_by_id("txt_1")
#Find Element By XPATH
browser.find_element_by_xpath("//input")
#Find Element By Link Text
browser.find_element_by_link_text("Products")
#Find Element By Link Text
browser.find_element_by_link_text("Products")
#Find Element By Partial Link Text
browser.find_element_by_partial_link_text('Sign')
#Find Element By Name
browser.find_elements_by_name('foo')
#Find Element By Tag Name
browser.find_elements_by_tag_name('Input')
#Find Element By Class Name
browser.find_elements_by_class_name('breadcrumb')
#Find Element By CSS Selector
browser.find_elements_by_css_selector('input[name="txt"]')
Anh Tester
Đường dẫu khó chân vẫn cần bước đi
Đời dẫu khổ tâm vẫn cần nghĩ thấu