selenium 常用操作汇总

https://www.cnblogs.com/Ting-light/p/9770908.html

1.隐式等待,智能等待

driver.implicitly_wait(20)

 

2.启动

driver=webdriver.Chrome() 

driver.get("https://www.baidu.com")

 

3.明确的等待50秒,而不抛出异常,直到指定元素出现

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

WebDriverWait(browser, 50).until(EC.presence_of_element_located((By.ID, 'LoginForm_username'))) 

 

4.设定浏览器启动模式

mobileEmulation = {'deviceName': 'iPhone 6'}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', mobileEmulation) #模拟手机UA启动
options.add_argument("start-maximized")
options.add_argument("--no-sandbox")
options.add_argument('--disable-gpu')
options.add_argument('window-size=1920x3000') #设置浏览器尺寸
# 创建驱动实例
driver = webdriver.Chrome(options=options)
# 设定浏览器坐标
driver.set_window_position(x=600, y=0)

 

5.执行JS

js = "$('.gapInput-keypad__math__keys').scrollTop(300)"

browser.execute_script(js)

 

6.设置浏览器弱网络

https://blog.csdn.net/qq_43577241/article/details/104475287

set_network_conditions(**network_conditions) 设置chrome网络竞争设置 ,network_conditions:dict类型,

例如 driver.set_network_conditions(offline=False,latency=5, #额外的延迟(毫秒)

                 download_throughput=500*1024,#最大吞吐量

                 upload_throughput=500*1024)#最大吞吐量

‘throughput’可以同时设置上传和下载的吞吐量

driver.set_network_conditions(offline=False,latency=1000,throughput=500 * 1024)

 

posted @ 2020-04-13 22:16  Ootori  阅读(350)  评论(0编辑  收藏  举报