python+selenium

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()
posted @ 2022-05-26 20:01  figo1421  阅读(46)  评论(0编辑  收藏  举报