from selenium import webdriver
from selenium.webdriver.edge.service import Service
from lxml import etree
from time import sleep
from selenium.webdriver.common.by import By
s = Service(r'./msedgedriver')
diver = webdriver.Edge(service=s)
diver.get('https://www.qcc.com/boss')
# 拿到数据
# page_text = diver.page_source
# tree = etree.HTML(page_text)
# name = tree.xpath('//*[@id="V3_Index_S"]/div/input[1]/@placeholder')[0]
# print(name)
# 找到搜索框输入文字
search_input = diver.find_element(By.ID, 'bossname')
search_input.send_keys('李国庆')
# 定位到按钮 点击事件
bt = diver.find_element(By.ID, 'V3_Search_bt')
bt.click()
sleep(2)
# 执行js代码
diver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(5)
diver.get('https://www.baidu.com')
diver.back()
sleep(3)
diver.forward()
diver.close()
iframe
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
s = Service(r'./msedgedriver')
diver = webdriver.Edge(service=s)
diver.get('https://www.runoob.com/try/try.php?filename=jqueryui-example-droppable')
diver.switch_to.frame('iframeResult')
btn = diver.find_element(By.ID, 'draggable')
action = ActionChains(diver)
action.click_and_hold(btn)
for i in range(5):
action.move_by_offset(17, 0).perform()
sleep(0.5)
action.release()
diver.close()
模拟登录
# -*- codeing = utf-8 -*-
# @Author: xiaodunmeng
# @File: testlogin.py
# @Software: PyCharm
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
s = Service(r'./msedgedriver')
diver = webdriver.Edge(service=s)
diver.get('https://qzone.qq.com/')
diver.switch_to.frame('login_frame')
bt_tag = diver.find_element(By.ID, 'switcher_plogin')
bt_tag.click()
userName_tag = diver.find_element(By.ID, 'u')
passWord_tag = diver.find_element(By.ID, 'p')
sleep(1)
userName_tag.send_keys('807031646')
sleep(1)
passWord_tag.send_keys('qq13660422317')
sleep(1)
subtn = diver.find_element(By.ID, 'login_button')
subtn.click()
sleep(3)
diver.close()
edge浏览器无头模式
环境变量配置 /opt/homebrew/bin/python3.9
将driver 放入bin中
from selenium import webdriver
from time import sleep
from selenium.webdriver.edge.options import Options
edge_options = Options()
edge_options.add_argument('headless')
edge_options.add_argument('--disable-gpu')
edge_options.add_experimental_option('excludeSwitches', ['enable-automation'])
bro = webdriver.Edge(options=edge_options)
# 无头浏览器
bro.get('https://www.baidu.com')
print(bro.page_source)
sleep(2)
bro.close()
chorme无头模式
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
bro = webdriver.Chrome(options=chrome_options)
bro.get('https://www.baidu.com')
print(bro.page_source)
sleep(2)
bro.quit()