2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Download chrome: You can use the software that comes with your computer [HUES, 360, etc.] to download
Download chromedriver:
Old version address: http://chromedriver.storage.googleapis.com/index.html, click in, there is a notes.txt file to view the supported versions
The latest version: https://googlechromelabs.github.io/chrome-for-testing/#stable, open and find the corresponding download address
If the above two versions cannot find the corresponding version driver, see the following address:
https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json
Press Ctrl+F to search for the corresponding version and find the download address of the version close to it. It is recommended to format it in json format for easy finding.
May be used to install the required import and export packages
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt
Browser default settings can remove automatic control prompts
# 获取配置对象
option = webdriver.ChromeOptions()
option.add_experimental_option("detach", True)
# 去掉自动化标识
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument('--disable-blink-features=AutomationControlled')
# 关闭保存密码弹窗
prefs = {}
prefs['credentials_enable_service'] = False
prefs['profile.password_manager_enabled'] = False
option.add_experimental_option('prefs', prefs)
# 设置生效
driver2 = webdriver.Chrome(options=option)
time.sleep(1)
driver2.set_window_size(400, 700) #模拟小程序
# 关闭当前窗口页面
driver.close()
# 关闭所有窗口页面
driver.quit()
# input类型上传框,==文件上传==
driver2.find_elements(By.CLASS_NAME, 'icon-tianjia')[0].click()
time.sleep(1)
driver2.find_element(By.XPATH, '//*[@type="file"]').send_keys('C:\Users\xxx\Desktop\Test_photo\20240624-164909.jpg')
time.sleep(2)
queren = driver2.find_element(By.XPATH, '//div[@role="dialog" and @aria-labelledby="确定要发送这张照片吗"]/div/button/div/span[text()="确认"]')
driver2.execute_script("arguments[0].click();", queren)
time.sleep(2)
# 发送 - 视频
driver2.find_elements(By.CLASS_NAME, 'icon-tianjia')[0].click()
time.sleep(1)
driver2.find_element(By.XPATH, '//*[@type="file"]').send_keys('C:\Users\xxx\Desktop\Test_photo\20240627-104157.mp4')
time.sleep(2)
#Absolute path, select from the root node
web.find_element(By.XPATH, ‘/html/body/div/div/div[3]/a’).click()
#Relative path, starting from any node, often used with attribute positioning to select labels, the format is as follows:
web.find_element(By.XPATH, ‘//input[@id=“kw”]’).send_keys(‘ok’)
#Multi-attribute combination positioning
web.find_element(By.XPATH, ‘//input[@id=“kw” and @name=“wd” and @class=“s_ipt”]’).send_keys(‘ok’)
#Multiple sets of data are positioned using subscripts
web.find_element(By.XPATH, ‘//div[@id=“s-top-left”]/a[4]’).click()
#Locate the parent element of an element and use /... to indicate the parent tag of a tag
web.find_element(By.XPATH, ‘//div[@id=“s-top-left”]/…’).click()
#text equals
web.find_element(By.XPATH, '//a[text()="文库"]').click()
#Text contains
web.find_element(By.XPATH, ‘//a[contains(text(),“文”)]’).click()
# Tags below the same level
web.find_element(By.XPATH, '//a[text()="文库"]/following-sibling::a[3]').click()
#Labels above the same level
web.find_element(By.XPATH, '//a[text()="文库"]/preceding-sibling::a[3]').click()
- By.LINK_TEXT
web.find_element(By.LINK_TEXT, 'Is religion really declining in modern society? [Thinking Laboratory]').click()
- By.PARTIAL_LINK_TEXT
web.find_element(By.PARTIAL_LINK_TEXT, 'Is religion really declining in modern society?').click()
- By.CLASS_NAME
When multiple elements with the same class are located by find_element(By.CLASS_NAME), the first one is selected by default.
web.find_elements(By.CLASS_NAME, ‘channel-link’)[4].click()
Not available when there are multiple class attributes
- By.TAG_NAME
- By.NAME
- By.CSS_SELECTOR
# 根据id: web.find_element(By.CSS_SELECTOR, '#kw').send_keys('Python') # 根据class: web.find_element(By.CSS_SELECTOR, '.nav-search-input').send_keys('Python') # 输入框标签:<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off"> # 根据name属性定位输入框 web.find_element(By.CSS_SELECTOR, '[name="wd"]') # a标签(属性筛选了一部分):<a href="http://image.baidu.com/">图片</a> # 根据href属性定位 web.find_element(By.CSS_SELECTOR, 'a[href="http://image.baidu.com/"]') # 根据href属性模糊匹配-包含 web.find_element(By.CSS_SELECTOR, 'a[href*="baidu.com/"]') # 根据href属性模糊匹配-匹配开头 web.find_element(By.CSS_SELECTOR, 'a[href^="http://image"]') # 根据href属性模糊匹配-匹配结尾 web.find_element(By.CSS_SELECTOR, 'a[href$="baidu.com/"]') # 组合定位 # 输入框标签(属性筛选了一部分):<input class="nav-search-input"> # 组合定位class web.find_element(By.CSS_SELECTOR, 'input.nav-search-input') # 输入框标签(属性筛选了一部分):<input id="nav-search-input"> # 组合定位id web.find_element(By.CSS_SELECTOR, 'input#nav-search-input')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
## not(contains(@style,"display")),元素定位,尽量使用相对定位,找到有id的父级或者兄弟级别 ## following-sibling::div[1],兄弟级别下一个div ## rightClick = ActionChains(driver) # 实例化ActionChains类,鼠标右键操作 ## rightClick.context_click(logo).perform() # context_click(logo)在logo上执行右键操作, # 回复 //div[text()="999999999"] logo = driver.find_element(By.XPATH, '//div[text()="999999999"]') rightClick = ActionChains(driver) # 实例化ActionChains类 rightClick.context_click(logo).perform() # context_click(logo)在logo上执行右键操作,perform()是一个执行动作 # 回复按钮 //div[@class="ivu-select-dropdown ivu-dropdown-transfer"]/ul/li driver.implicitly_wait(5) time.sleep(2) driver.find_element(By.XPATH, '//div[@class="ivu-select-dropdown ivu-dropdown-transfer"]/ul/li').click() time.sleep(2) # 回复-庆祝 driver.find_element(By.XPATH, '//div[@class="chatRoom" and @style!="display: none;"]/div[2]/div[3]/textarea').send_keys("[庆祝]") time.sleep(1) driver.find_element(By.XPATH, '//div[@class="chatRoom" and @style!="display: none;"]/div[2]/div[3]/button[2]').click() # #点击-图片 driver.find_element(By.XPATH, '//div[@class="chatRoom" and @style!="display: none;"]/div[2]/div[2]/div[2]/div/div/i').click() driver.implicitly_wait(5) time.sleep(2) # 发送图片 driver.find_element(By.XPATH, '//div[@class="ivu-select-dropdown ivu-dropdown-transfer"]/ul/div/div/*[@type="file"]').send_keys('C:\Users\duxiaowei\Desktop\Test_photo\20240624-164909.jpg') driver.implicitly_wait(5) time.sleep(2) # 优惠券 driver.find_element(By.XPATH, '//div[@class="chatRoom" and @style!="display: none;"]/div[2]/div[2]/div[16]/i').click() driver.implicitly_wait(5) time.sleep(2) # 弹窗, //div[@class="ivu-modal-mask" and not(contains(@style,"display"))] driver.find_element(By.XPATH, '//div[@class="ivu-modal-mask" and not(contains(@style,"display"))]/following-sibling::div[1]/div/div/div[2]/div/textarea').send_keys("http://www.bilibili.com") driver.implicitly_wait(5) time.sleep(2) # 发送-确定 driver.find_element(By.XPATH, '//div[@class="ivu-modal-mask" and not(contains(@style,"display"))]/following-sibling::div[1]/div/div/div[3]/div/button').click() driver.implicitly_wait(5) time.sleep(2)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
# 鼠标右键 from selenium.webdriver import ActionChains # 鼠标右键操作模拟 chehui = driver.find_element(By.XPATH, '//*[text()="我是小海呀2"]') rightClick = ActionChains(driver) # 实例化ActionChains类 time.sleep(2) rightClick.context_click(chehui).perform() # context_click()执行右键操作,perform()是一个执行动作 time.sleep(2) driver.find_element(By.XPATH, '//div[not(contains(@style,"display"))]/ul/li[text()="撤回"]').click()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
driver.find_element(By.XPATH, '//span[text()="评"]/parent::button/preceding-sibling::div/div/button/span/i[contains(@class,"image")]').click()
- 1
web自动化初中级,面试题
1.如果web页面加载元素过慢,通常怎么处理?
要点:增加等待时间,
可通过sleep,wait,自定义方法等
或者说显示等待,隐式等待等
2.动态元素如何定位?
要点:先找它的父级,或者同级元素,再去定位它
3.自动化过程中常见的异常能说几个吗?
例如:
定位不到元素:NoSuchElementException
超时异常:TimeoutException
没有这个属性:NoSuchAttributeException
元素不可见:ElementNotVisibleException
能说1-2个就可以.
4. 有没有处理过文件上传?
做过自动化的话,大多数项目都有会文件上传.
可以不需要说出如何上传,回答是否处理过就可以.
5. web自动化常用定位方式有哪几种?
xpath,CSS定位,id,name,class_name,tag_name,link_text,partial_link_text
说出3个基本就用过,尤其是要包含xpath,CSS定位,id
6. 自动化测试通常分哪几种?各有什么优劣势,应用场景有哪些?
常见3种:API自动化,web自动化,app自动化
API自动化:稳定性高,维护成本低,可用场景多,通常会优先考虑.
web自动化:主要针对web端,注重页面交互.
app自动化:最为复杂,考虑因素多,会有跨平台ios/安卓,兼容性(屏幕尺寸/系统版本等)考虑,相对较重,维护成本高.
自动化的应用场景:主要是用于回归测试
意思对即可.