VM虛擬機器安裝CentOS7
阿新 • • 發佈:2022-05-23
import sys
from selenium.webdriver.support.select import Select
from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from scrapy.selector import Selector
from selenium.webdriver.common.by import By
import time
from selenium.webdriver import ActionChains, Keys
class Use_Selenium(object):
def __init__(self, url, model=None):
self.bw = webdriver.Chrome()
self.bw.implicitly_wait(3)
self.url = url
self.model = model
self.bw.get(self.url)
if self.model == 'max':
self.bw.maximize_window()
self.get_bw()
def get_bw(self):
return self.bw
bw = Use_Selenium('http://sahitest.com/demo/clicks.htm').get_bw()
we = bw.find_element(By.XPATH, '/html/body/form/input[4]')
actions = ActionChains(bw).context_click(we)
time.sleep(3)
actions.perform()
wait = WebDriverWait(bw, 3)
wait.until(EC.text_to_be_present_in_element((By.ID, 'id2'), 'id 2'))
print(bw.find_element(By.ID, 'id2').text)
# 視窗切換
we = bw.find_element(By.XPATH, '/html/body/form/input[2]')
sel = Select(we)
sel.select_by_value('o2val')
sel.select_by_index(2)
sel.s
# 開啟視窗
bw = webdriver.Chrome()
# 隱式等待
bw.implicitly_wait(3)
# 切換frame
bw.switch_to.default_content()
bw.switch_to.frame('name')
# 訪問網頁
bw.get('https://cn.bing.com/')
# 最大化最小化
bw.maximize_window()
bw.minimize_window()
# 八大元素定位
bw.find_element(By.ID, '圖')
bw.find_element(By.NAME, '圖')
bw.find_element(By.CSS_SELECTOR, '圖')
bw.find_element(By.XPATH, '圖')
bw.find_element(By.LINK_TEXT, '圖')
bw.find_element(By.PARTIAL_LINK_TEXT, '圖')
bw.find_element(By.TAG_NAME, '圖')
bw.find_element(By.CLASS_NAME, '圖')
# webdriver屬性
bw.name
bw.current_url
bw.title
bw.page_source
bw.current_window_handle
bw.window_handles
# webdriver方法
bw.back()
bw.forward()
bw.refresh()
bw.close()
bw.quit()
bw.switch_to
# webelement屬性
we = bw.find_element(By.ID, 'c0')
we.id
we.size
we.rect
we.tag_name
we.text
# webelement方法
we = bw.find_element(By.ID, 'c0')
we.send_keys()
we.clear()
we.get_attribute('name')
we.is_selected()
we.is_enabled()
we.is_displayed()
we.value_of_css_property('')
# webelement-Select下拉框
we = bw.find_element(By.ID, 'c0')
sel = Select(we)
sel.select_by_visible_text()
sel.select_by_index()
sel.select_by_value()
sel.options.
sel.all_selected_options
# 處理彈窗alert(只有確定),confirm(確定取消),prompt(確定取消輸入)
tc = bw.switch_to.alert
tc.accept()
tc.dismiss()
tc.text
tc.send_keys()
# 等待
time.sleep()
bw.implicitly_wait()
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(bw, 3)
wait.until(EC.text_to_be_present_in_element((By.ID, 'id2'), 'id 2'))
bw.find_element(By.ID, 'id2').text
# 視窗切換
for w in bw.window_handles:
bw.switch_to.window(w)
# 滑鼠
from selenium.webdriver import ActionChains
actions = ActionChains(bw).double_click(we)
actions.perform()
# 滑鼠
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import Keys
bw = Use_Selenium('https://www.baidu.com', 'max').get_bw()
we = bw.find_element(By.ID, 'kw')
we.send_keys('123')
time.sleep(2)
we.send_keys(Keys.CONTROL,'a')
we = bw.find_element(By.LINK_TEXT, '新聞')
time.sleep(2)
ActionChains(bw).move_to_element(we).perform()
we.click()
# JS
bw = Use_Selenium('https://3w.huanqiu.com/a/24d596/489Ycyt1P53', 'max').get_bw()
time.sleep(5)
# js = 'window.scrollTo(0, document.body.scrollHeight)'
js = 'window.scrollTo(0, 10000)'
bw.execute_script(js)
bw.find_element(By.PARTIAL_LINK_TEXT, '圖').click()
bw.find_element(By.XPATH, "//*[starts-with(@class,'id')]").click()
bw.find_element(By.XPATH, "//*[substring(@class,2)='ff']").click()
bw.find_element(By.XPATH, "//*[contains(@class,'id')]").click()
bw.find_element(By.XPATH, "//*[text()='abc']").click()
bw.find_element(By.XPATH, "//*[text()='abc']").get_attribute('href')
bw.find_element(By.LINK_TEXT, '圖片').click()
# 下拉框選擇
sel = Select(bw.find_element(By.LINK_TEXT, '圖片'))
sel.select_by_visible_text()
# 處理彈窗alert(只有確定),confirm(確定取消),prompt(確定取消輸入)
ale = bw.switch_to.alert
ale.accept()