- Obtain information element
1. text attributes, displaying the element displayed in the web page text
2. get_attribute method
Gets the value of a property:
element.get_attribute (element properties)
element.get_attribute('href')
element.get_attribute('style')
Element corresponding to obtain the HTML source code:
element.get_attribute('outerHTML')
Get the inner portion of the HTML source code elements:
element.get_attribute('innerHTML')
- BeautifulSoup4 library
Frequency rarely used in this way, we can introduce Baidu for more information.
- Hermit waiting time
driver.implicitly_wait(10)
- Display wait
from selenium.webdriver.support.ui import WebDriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
ele = WebDriverWait(driver,60)
until(EC.presence_of_element_located((By.ID,'username')))
# Wait for 60s, appears to know an element, id as username elements
For example:
# Is the id element 1 is first set to wait for the Hermit = 60 seconds, regardless found not found, Hermit waiting time will reset to 10 seconds driver.implicitly_wait (60 ) the try : ELE = driver.find_element_by_id ( ' 1 ' ) Print (ELE .text) IF ele.text.startswith ( ' loose ground network - software testing-ground ' ) Print ( ' Pass ' ) the else : Print ( ' Fail ' ) the except : Print ( ' Exception happend ' ) a finally : driver.implicitly_wait(10)
- frame processing: Web pages embedded, frame / iframe
Switch frame
driver.switch_to.frame(frame_reference)
1. frame element attribute name or attribute ID
2. The index value (starting from 0)
3. frame corresponding WebElement: driver.find_element_by_tag_name ( "iframe")
Switching back to the main html which: driver.switch_to.default_content ()
- String elements Clear
clear方法 : input1.clear()
- Gets contents of the input text box
get_attribute (): Get the text content input elements inside the input
input1.get_attribute('value')
- Single box
type = "radio"
click () method selection: whether the original element is selected, and click on the element does not go directly to the questions can be sure that the check box is selected
- Check box
type = "checkbox"
click () method to select
is_selected () method to obtain the status of a selected:
True: has been selected
False: unchecked
- Check box
select, there may be multiple multiple choice, do not write the radio
Select class of selenium may be used
Importing classes: from selenium.webdriver.support.ui import Select
Methods deselect_all (): clear out all selected elements
方法select_by_visible_text("值"):根据复选框中文本信息选中
实例如下:
#导入Select类 from selenium.webdriver.support.ui import Select #获得相应的WebElement select = Select(driver.find_element_by_id("multi")) #去除选择所有的选项 select.deselect_all() select.select_by_visible_text("雅阁") select.select_by_visible_text("宝马 740") #获得相应的WebElement select = Select(driver.find_element_by_id("single")) select.select_by_visible_text("男")
- 获取标题
driver.title
- 获取当前url
driver.current_url
- 简单的断言
assert driver.title == "松勤_百度搜索" --- 当前浏览器的标题是否是“松勤_百度搜索”
- 截屏
driver.get_screenshot_as_file('ssl.png') 括号里面填写文件名称,也可以固定保存路径
- 获取某一个元素当前的图片
ele=driver.find_element_by_cas_selector('img[class="d-flag"]')
ele.screenshot(r'D:\yun.png')