+1

Part5- Một số thuộc tính của Selenium Python Bindings

Ở phần 4 chúng ta đã biết về Page Objects, Page object classes, Page elements, Locators, WebDriver API và test case thông qua bài viết

https://viblo.asia/huong.quynh/posts/gVQeljQVvZJ

Bài viết này sẽ cung cấp tiếp một số thuộc tính như dưới đây

1. Action Chains

Action Chains là một cách để tự động hóa tương tác như là hành động di chuyển chuột, nhấn phím, hoặc tương tác với menu. Điều này rất hữu ích cho những action phức tạp hơn như di chuột qua và kéo và thả.

class selenium.webdriver.common.action_chains.ActionChains(driver)

Khi bạn gọi phương thức trên đối tượng ActionChains, các hành động được lưu trữ trong một hàng đợi trong các đối tượng ActionChains. Khi bạn gọi perform (), các sự kiện được sắp xếp theo thứ tự.

ActionChains có thể sử dụng trong một chain pattern

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()

Hoặc có thể sắp xếp từng hành động một sau đó thực hiện

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()

Một số câu lệnh giữ chuột trái, right-click và double-click vào element

Câu lệnh giữ chuột trái

click_and_hold(on_element=None)

on_element: Element để di chuột xuống, nếu không thì sẽ click vào vị trí hiện tại

Thực hiện một context-click ( right click) trên một element

context_click(on_element=None)

Double-clicks một element.

double_click(on_element=None)

on_element: Element để thực hiện double-click. Nếu không thì sẽ click vào vị trí hiện tại.

2. Sử dụng ChromeDriver như thế nào?

Download ChromeDriver bản mới nhất unzip chromedriver_linux32_x.x.x.x.zip

Bây giờ bạn có thể tạo một ví dụ của Chrome Driver như sau:

driver = webdriver.Chrome(executable_path="/path/to/chromedriver")

Lúc này bạn có thể nhìn thấy ChromeDriver được thực thi

3. Desired Capabilities

Desired Capabilities thực hiện như sau:

class selenium.webdriver.common.desired_capabilities.DesiredCapabilities

Câu lệnh trên sử dụng như là điểm bắt đầu cho việc tạo một đối tượng Desired Capabilities cho yêu cầu remote webdrivers để kết nối tới selenium server hoặc selenium grid

Ví dụ:

from selenium import webdriver

selenium_grid_url = "http://198.0.0.1:4444/wd/hub"

# Create a desired capabilities object as a starting point.
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['platform'] = "WINDOWS"
capabilities['version'] = "10"

# Instantiate an instance of Remote WebDriver with the desired capabilities.
driver = webdriver.Remote(desired_capabilities=capabilities,
                          command_executor=selenium_grid_url)

4. Selenium 2 có hỗ trợ XPath 2.0 không ?

Selenium 2 hỗ trợ Xpath và hỗ trợ bất cứ trình duyệt nào, trong những trình duyệt không có XPath(IE6,7,8) thì Selenium chỉ hỗ trợ XPath 1.0

5. How to scroll down to the bottom of a page ?

Bạn có thể sử dụng phương thức execute_script để thực hiện javascript trong khi load page, vì vậy bạn có thể gọi tơi JavaScript API để scroll xuống hoặc tới bắt kỳ vị trí nào của trang. Đây là ví dụ để scroll xuống dưới cùng của trang

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

Đối tượng window trong DOM có một phương thức scroll để scroll tới bất kỳ vị trí nào của một cửa sổ đang mở. scrollHeight là thuộc tính chung cho tất cả elements

6. How to auto save files using custom Firefox profile ?

Bước đầu tiên là xác định loại file bạn muốn tự động lưu

Để xác định nội dung bạn muốn download tự động bạn có thể sử dụng curl:

curl -I URL | grep "Content-Type"

Cách khác để tìm nội dung đang sử dụng requests module, bạn có thể sử dụng nó như:

import requests
content_type = requests.head('http://www.python.org').headers['content-type']
print(content_type)

Một khi nội dung được xác định, bạn có thể sử dụng nó để set cho firefox profile preference

browser.helperApps.neverAsk.saveToDisk

Dưới đây là ví dụ:

import os

from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")

browser = webdriver.Firefox(firefox_profile=fp)
browser.get("http://pypi.python.org/pypi/selenium")
browser.find_element_by_partial_link_text("selenium-2").click()

Ở ví dụ trên thì application/octet-stream được sử dụng như là content type và browser.download.dir xác định thư mục bạn muốn download files

7. How to use firebug with Firefox ?

Đầu tiên download Firebug XPI file, sau đó bạn gọi tới phương thức add_extension có sẵn cho firefox profile:

from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.add_extension(extension='firebug-1.8.4.xpi')
fp.set_preference("extensions.firebug.currentVersion", "1.8.4") #Avoid startup screen
browser = webdriver.Firefox(firefox_profile=fp)

8. How to take screenshot of the current window ?

Sử dụng phương thức save_screenshot cung cấp bởi webdriver:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://www.python.org/')
driver.save_screenshot('screenshot.png')
driver.quit()

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí