Selenium 模块2_iframe处理_动作链

先实例化


from selenium import webdriver
#实例化一个浏览器对象,获取网址
web_requests=webdriver.Chrome(executable_path='./chormedriver')
web_requests.get('')

iframe 动作链是什么?

iframe 一个标签
动作链 模拟人类的拖动的操作

如果定位的标签是存在于iframe标签之中 则必须通过如下操作在进行标签定位

#锁定iframe作用域
#switch_to.frame参数是id
web_requests.switch_to.frame('id')#切换浏览器标签定位的作用域

动作链

from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
#实例化一个浏览器对象,获取网址
web_requests=webdriver.Chrome(executable_path='./chormedriver')
web_requests.get('https:xxxx.com/')
#锁定iframe作用域
#switch_to.frame参数是id
web_requests.switch_to.frame('id')#切换浏览器标签定位的作用域
div=web_requests.find_element_by_id('draggable')

#动作链实例化
action=ActionChains(web_requests)
#点击长按指定标签
action.click_and_hold(div)
for i in(range(5)):

    #分5次,每次向右移动17个像素单位
    #move_by_offset(x,y)x水平 ,y竖直
    action.move_by_offset(17,0).perform()#perform()立即执行动作链操作
    #暂停一下0.2秒能看清楚
    sleep(0.2)
#释放动作链接
action.release()

selenium QQ空间登录实例

from selenium import webdriver
from  time import sleep
#实例化
bro=webdriver.Chrome('./chromedriver.exe')
#网址
bro.get('https://qzone.qq.com')
#定位
bro.switch_to.frame('login_frame')
a_tag=bro.find_element_by_id('switcher_plogin')
a_tag.click()
urerName_tag=bro.find_element_by_id('u')
passWord_tag=bro.find_element_by_id('p')
#账户
urerName_tag.send_keys('123')
#密码
passWord_tag.send_keys('456')
btn=bro.find_element_by_id('login_button')
btn.click()
sleep(3)
bro.quit()

无头浏览器_规避检测

from selenium import webdriver
#实现无可视化界面的
from selenium.webdriver.chrome.options import Options
#实现规避检测
from selenium.webdriver import ChromeOptions


#实现无可视化界面操作
chrome_options=Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--dissable-gpu')
#实现规避检测
option=ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
#实例化
bro=webdriver.Chrome('./chromedriver.exe',chrome_options=chrome_options,options=option)
#网址
bro.get('https://www.baidu.com')
print(bro.page_source)

原文地址:https://www.cnblogs.com/SkyRabbit/p/13704751.html