Python selenium

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaodunmeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值