Selenium base method

http://selenium-python.readthedocs.io/locating-elements.html

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

Selenium support.expected_conditions

先翻譯一下這些方法的用法

  • title_is: 判斷當前頁面的title是否精確等於預期

  • title_contains: 判斷當前頁面的title是否包含預期字符串

  • presence_of_element_located: 判斷某個元素是否被加到了dom樹里,並不代表該元素一定可見

  • visibility_of_element_located: 判斷某個元素是否可見.可見代表元素非隱藏,並且元素的寬和高都不等於0

  • visibility_of: 跟上面的方法做一樣的事情,只是上面的方法要傳入locator,這個方法直接傳定位到的element就好了

  • presence_of_all_elements_located: 判斷是否至少有1個元素存在於dom樹中。舉個例子,如果頁面上有n個元素的class都是'column-md-3',那麼只要有1個元素存在,這個方法就返回True

  • text_to_be_present_in_element: 判斷某個元素中的text是否包含了預期的字符串

  • text_to_be_present_in_element_value: 判斷某個元素中的value屬性是否包含了預期的字符串

  • frame_to_be_available_and_switch_to_it: 判斷該frame是否可以switch進去,如果可以的話,返回True並且switch進去,否則返回False

  • invisibility_of_element_located: 判斷某個元素中是否不存在於dom樹或不可見

  • element_to_be_clickable: 判斷某個元素中是否可見並且是enable的,這樣的話才叫clickable

  • staleness_of: 等某個元素從dom樹中移除,注意,這個方法也是返回True或False

  • element_to_be_selected: 判斷某個元素是否被選中了,一般用在下拉列表

  • element_selection_state_to_be: 判斷某個元素的選中狀態是否符合預期

  • element_located_selection_state_to_be: 跟上面的方法作用一樣,只是上面的方法傳入定位到的element,而這個方法傳入locator

  • alert_is_present: 判斷頁面上是否存在alert

原文網址:https://read01.com/Nye7J.html

Basic Login

driver = webdriver.Chrome()
driver.get("http:///en-gb/")
driver.find_element_by_name("username").clear()
driver.find_element_by_name("username").send_keys("")
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys("")
driver.find_element_by_xpath("//button[@type='submit']").click()
driver.quit()

可以看到用上面的方法可以定位到你要的物件 但是有發現一些問題吧? 因為你的locate 不一定是by Name or Xpath 所以每次在寫的時候上下都用不一樣的方法

Login with Selenium Base Class

透過這個方法讓他動態傳入loc

element = WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))

改成 下列這樣 跟上面差異在哪呢?

import CommonSeleniumBaseClass
from selenium.webdriver.common.by import By
from selenium import webdriver
username_loc =(By.NAME,'username')
password_loc =(By.NAME,'password')
submit_loc =(By.XPATH,"//button[@type='submit']")
customDriver = CommonSeleniumBaseClass.SeleniumBaseClass(webdriver.Chrome())
customDriver.open("http:///en-gb/")
customDriver.send_key_with_Element(username_loc,"")
customDriver.send_key_with_Element(password_loc,"")
customDriver.click_with_Element(submit_loc)

Select Option and Switch Window

import CommonSeleniumBaseClass
from selenium.webdriver.common.by import By
from selenium import webdriver
username_loc =(By.NAME,'username')
password_loc =(By.NAME,'password')
submit_loc =(By.XPATH,"//button[@type='submit']")
timeOut_loc = (By.ID,"TimeOutPeriod")
responsibleGaming = (By.XPATH,"//a[@window-open='resgame']")
responsibleGaming = (By.XPATH,"//a[@window-open='resgame']")
driver = webdriver.Chrome()
customDriver = CommonSeleniumBaseClass.SeleniumBaseClass(driver)
customDriver.open("http://new.star.avabet.com/en-gb/")
customDriver.send_key_with_Element(username_loc,"mmx362003")
customDriver.send_key_with_Element(password_loc,"Xuenn2016")
customDriver.click_with_Element(submit_loc)
customDriver.click_with_Element(responsibleGaming)
#customDriver.clickElementsBySendKey(responsibleGaming,"\n")
title = customDriver.switchNewWindow()
customDriver.selectElementByValue(timeOut_loc,"7")

各位可以發現為什麼突然發現 這個座標的item不能點了呢? 怎麼解決呢?

http://stackoverflow.com/questions/11908249/debugging-element-is-not-clickable-at-point-error

Modal Dialog

import CommonSeleniumBaseClass
from selenium.webdriver.common.by import By
from selenium import webdriver
username_loc =(By.NAME,'username')
password_loc =(By.NAME,'password')
submit_loc =(By.XPATH,"//button[@type='submit']")
confirmButton_loc =(By.XPATH,"//button[@translate='txtBtnComOk']")
customDriver = CommonSeleniumBaseClass.SeleniumBaseClass(webdriver.Chrome())
customDriver.open("http:///en-gb/")
customDriver.send_key_with_Element(username_loc,"")
customDriver.send_key_with_Element(password_loc,"")
customDriver.click_with_Element(submit_loc)
customDriver.click_with_Element(confirmButton_loc)

Basic Banking Page Switch Iframe

import CommonSeleniumBaseClass
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
username_loc =(By.NAME,'username')
password_loc =(By.NAME,'password')
submit_loc =(By.XPATH,"//button[@type='submit']")
banking_loc = (By.ID,"toggle-banking")
deposit_loc = (By.XPATH,"//a[@translate='navBankingDeposit']")
iframe_loc = (By.XPATH,"//iframe[@class='container']")
nLBT_loc = (By.XPATH,'//*[contains(text(),"NLBT_CN")]')
customDriver = CommonSeleniumBaseClass.SeleniumBaseClass(webdriver.Chrome())
customDriver.open("http:///en-gb/")
customDriver.send_key_with_Element(username_loc,"")
customDriver.send_key_with_Element(password_loc,"")
customDriver.click_with_Element(submit_loc)
customDriver.click_with_Element(banking_loc)
customDriver.click_with_Element(deposit_loc)
customDriver.switchIframe(iframe_loc)
customDriver.click_with_Element(nLBT_loc)

Action Chains

http://selenium-python.readthedocs.io/api.html

Reference http://blog.csdn.net/huilan_same/article/details/52305176 用selenium做自動化,有時候會遇到需要模擬鼠標操作才能進行的情況,比如單擊,雙擊,點擊鼠標右鍵,拖拉等等。而selenium給我們提供了一個類來處理這類事件 - ActionChains

1.ActionChains基本用法

首先需要了解ActionChains的執行原理,當你調用ActionChains的方法時,不會立即執行,而是會將所有的操作按順序存放在一個隊列裡,當你調用perform()方法時,隊列中的時間會依次執行。

這種情況下我們可以有兩種調用方法:

鍊式寫法

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()

分步寫法

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()

兩種寫法本質是一樣的,ActionChains都會按照順序執行所有的操作。

2.ActionChains方法列表

  • 點擊(on_element = None) - 單擊鼠標左鍵
  • click_and_hold(on_element = None) - 點擊鼠標左鍵,不鬆開
  • context_click(on_element = None) - 點擊鼠標右鍵
  • double_click(on_element = None) - 雙擊鼠標左鍵
  • drag_and_drop(source,target) - 拖拉到某個元素然後鬆開
  • drag_and_drop_by_offset(source,xoffset,yoffset) - 拖拉到某個坐標然後鬆開
  • key_down(value,element = None) - 按下某個鍵盤上的鍵
  • key_up(value,element = None) - 鬆開某個鍵
  • move_by_offset(xoffset,yoffset) - 鼠標從當前位置移動到某個坐標
  • move_to_element(to_element) - 鼠標移動到某個元素
  • move_to_element_with_offset(to_element,xoffset,yoffset) - 移動到距一個元素(左上角坐標)多少距離的位置
  • perform() - 執行鏈中的所有動作
  • release(on_element = None) - 在某個元素位置鬆開鼠標左鍵
  • send_keys(* keys_to_send) - 發送某個鍵到當前焦點的元素
  • send_keys_to_element(element,* keys_to_send) - 發送某個鍵到指定元素

Hover and click the button

hoverElement = ActionChains(self.driver).move_to_element(element) hoverElement.perform()

Excute Javascript

捲動頁面

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

Reference: https://read01.com/Nye7J.html http://www.php.cn/python-tutorials-353210.html http://blog.csdn.net/huilan_same/article/details/52305176

results matching ""

    No results matching ""