Web测试入门:Selenium+Chrome+Python+Mac OS
1、如何寻找页面元素
https://blog.csdn.net/weixin_43595001/article/details/84204651
右键-检查-element-左上角带小箭头的方形-选择“提交反馈”,就可以在代码中找到提交反馈
type=“button” button是name,可以使用find_element_by_name。多个方法,看看怎么用。
学习链接1: https://www.cnblogs.com/juking/p/9853612.html
- Chromedriver 下载及环境配置
1.1:打开chrome的设置,查看chrome版本。如果不是最新,升级到最新。
chrome://settings/help
1.2 下载与本机chrome浏览器对应的驱动程序
从http://chromedriver.storage.googleapis.com/index.html网址中下载与本机chrome浏览器对应的驱动程序,驱动程序名为chromedriver;
• 解压chromedriver,并将其mv 到/usr/local/bin下(目录下必须要有对应版本的driver);
打开Mac终端terminal : 进入 Downloads目录,输入命令: sudo cp -r chromedriver /usr/local/bin/(bin后面的“/”不能少!!!)
1.3 查看chromedriver 版本 chromedriver --version
弹出报错:mac不信任该软件
打开系统偏好设置-安全性与隐私-通用
直到可以查看chromedriver 版本 chromedriver --version
(base) eleme@E118435-BJ bin % chromedriver --version
ChromeDriver 93.0.4577.15 (660fc11082ba57405eca2e8c49c3e1af756fbfae-refs/branch-heads/4577@{#203})
2、安装selenium:执行 python -m pip install selenium
或者
python --version
sudo python get-pip.py
pip --version
pip install selenium
3、网上测试代码写入 sublime,修改文件名:test.py
coding = utf-8
from selenium import webdriver
from time import sleep, ctime
import os
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("MacOS")
driver.find_element_by_id("su").click()
sleep(5)
driver.quit()
4、进入 test.py 目录下 执行:python3 test.py
自动打开 chrome网页,5秒后关闭页面
学习链接
selenium打开chrome之后如何关闭全部窗口
https://bbs.csdn.net/topics/397542649
笔记:正常执行完代码后页面自动关闭;如果中间报错页面不关闭。
如何在Selenium Webdriver(Python)中找到包含特定文本的元素?
https://www.javaroad.cn/questions/22425
need_responses_hui =driver.find_elements_by_xpath("//*[contains(text(), ‘提交反馈’)]")
笔记:可以用来批量找到某元素对应的对象后,循环处理
笔记:以下用于解决元素无法点击的问题-
1、等待一会。
强制等待:效果好,但是耗时。适用于 页面刚刚打开,页面中不一定有寻找的元素:提交反馈
智能等待:效果好,不耗时。适用于页面肯定有这个元素,只是需要出现并点击。
2、使用更上一层的路径,避免同层级的元素干扰
3、重试几次。列表元素太多导致的时候。
Selenium 报错:element click intercepted: Element…is not clickable at point的解决办法
https://blog.csdn.net/weixin_44679832/article/details/104387860
Element is not clickable at point-----问题记录
https://www.cnblogs.com/luoyc/p/10578933.html
selenium等待处理
https://blog.csdn.net/wjd1994/article/details/79386597
python selenium 显示等待WebDriverWait与条件判断expected_conditions举例
https://blog.csdn.net/wycaoxin3/article/details/74017971?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.no_search_link
如何切换时间
日历这种抓取时间元素是按照数组位置,15号的位置是3行3列。每天执行昨天的case的时候需要更新时间
下拉到页面底部
https://blog.csdn.net/weixin_43632109/article/details/86797701
翻页
有下一页点击元素就可以点击,也是数组
```python
# coding = utf-8
from selenium import webdriver
from time import sleep, ctime
import os
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
options = Options()
options.headless = True
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 15)
driver.get()
sreach_window2=driver.current_window_handle
print('当前句柄3: ', sreach_window2) # 会打印所有的句柄
driver.implicitly_wait(2)
driver.find_element_by_class_name("next-icon-lock").click()
driver.find_element_by_id("account").send_keys("")
driver.find_element_by_id("password").send_keys("")
driver.find_element_by_xpath("/html/body/div[1]/div/div[1]/div[2]/div[2]/div[2]/div/form/div[3]/div/button").click()
n = driver.window_handles # 这个时候会生成一个新窗口或新标签页的句柄,代表这个窗口的模拟driver
print('当前句柄: ', n) # 会打印所有的句柄
driver.switch_to.window(n[-1]) # driver切换至最新生产的页面
sreach_window3=driver.current_window_handle
print('当前句柄3: ', sreach_window3) # 会打印所有的句柄
driver.implicitly_wait(10)
driver.find_element_by_id("appName").send_keys("")
driver.find_element_by_id("applyCreator").send_keys("")
#如果执行一次不行,多执行几次,被遮盖的问题
driver.find_element_by_xpath("/html/body/div/section/section/section/div/div/div/div[2]/form/div[2]/div/div/button").click()
sleep(10)
#driver.implicitly_wait(300)
driver.find_element_by_xpath("/html/body/div[1]/section/section/section/div/div/div/div[3]/div[2]/table/tbody/tr/td[1]/div/a").click()
#sleep(20)
#driver.find_element_by_xpath("/html/body/div/div/div/div[2]/div/div/div[2]/div/div/div[1]/div/div/div/ul/li[3]").click()
#/html/body/div/div/div/div[2]/div/div/div[2]/div/div/div[1]/div/div/div/ul/li[1]
#/html/body/div/div/div/div[2]/div/div/div[2]/div/div/div[1]/div/div/div/ul/li[2]
#/html/body/div/div/div/div[2]/div/div/div[2]/div/div/div[1]/div/div/div/ul/li[3]
n_ore = driver.window_handles # 这个时候会生成一个新窗口或新标签页的句柄,代表这个窗口的模拟driver
print('当前句柄4: ', n_ore) # 会打印所有的句柄
driver.switch_to.window(n_ore[1]) # driver切换至最新生产的页面
sreach_window_ore=driver.current_window_handle
print('当前句柄4: ', sreach_window_ore) # 会打印所有的句柄
#driver.implicitly_wait(200)
sleep(10)
#提交反馈
# 通过标签中文本的模糊匹配查找
#tmp = driver.find_elements_by_partial_link_text('提交反馈')
#查看历史反馈
#选择时间-前一天,不需要就注释
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[1]/div[1]/div/div[1]/div').click()
driver.implicitly_wait(10)
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/table/tbody/tr[3]/td[4]').click()
# /html/body/div[2]/div/div[2]/table/tbody/tr[2]/td[5]
#提交反馈
need_responses =driver.find_elements_by_xpath("//*[contains(text(), '提交反馈')]")
print('提交反馈-线上: ', need_responses) # 会打印所有的句柄
#将滚动条移动到页面的底部
js="var q=document.documentElement.scrollTop=100000"
driver.execute_script(js)
sleep(3)
for need_response in need_responses:
driver.implicitly_wait(10)
need_response.click()
driver.find_element_by_id('errorReason').send_keys("")
wait.until(ec.element_to_be_clickable((By.XPATH,"/html/body/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div[3]/div[2]/div/label[1]/span[1]"))).click()
wait.until(ec.element_to_be_clickable((By.XPATH,"/html/body/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div[4]/div[2]/div/label[1]/span[1]"))).click()
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[2]/div/div/form/div[2]/button[2]').click()
#如果有第二页
sleep(3)
wait.until(ec.element_to_be_clickable((By.XPATH,"/html/body/div/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[3]/div/div/button[2]"))).click()
sleep(10)
#提交反馈
need_responses_two =driver.find_elements_by_xpath("//*[contains(text(), '提交反馈')]")
print('提交反馈-线上第二页: ', need_responses_two) # 会打印所有的句柄
#将滚动条移动到页面的底部
js="var q=document.documentElement.scrollTop=100000"
driver.execute_script(js)
sleep(3)
for need_response_two in need_responses_two:
driver.implicitly_wait(10)
need_response_two.click()
driver.find_element_by_id('errorReason').send_keys("脚本写入:无影响")
wait.until(ec.element_to_be_clickable((By.XPATH,"/html/body/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div[3]/div[2]/div/label[1]/span[1]"))).click()
wait.until(ec.element_to_be_clickable((By.XPATH,"/html/body/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div[4]/div[2]/div/label[1]/span[1]"))).click()
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[2]/div/div/form/div[2]/button[2]').click()
sleep(10)
#
driver.find_element_by_xpath('/html/body/div/div/div/div[2]/div/div/div[2]/div/div/div[1]/div/div/div/ul/li[2]').click()
#driver.implicitly_wait(200)
sleep(10)
#选择时间-前一天,不需要就注释
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div[1]/div/div[1]/div').click()
driver.implicitly_wait(10)
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/table/tbody/tr[3]/td[5]').click()
sleep(10)
need_responses_hui =driver.find_elements_by_xpath("//*[contains(text(), '提交反馈')]")
print('提交反馈: ', need_responses_hui) # 会打印所有的句柄
#将滚动条移动到页面的底部
js="var q=document.documentElement.scrollTop=100000"
driver.execute_script(js)
sleep(3)
for need_response_hui in need_responses_hui:
driver.implicitly_wait(10)
need_response_hui.click()
driver.find_element_by_id('errorReason').send_keys("脚本写入:无影响")
wait.until(ec.element_to_be_clickable((By.XPATH,"/html/body/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div[3]/div[2]/div/label[1]/span[1]"))).click()
wait.until(ec.element_to_be_clickable((By.XPATH,"/html/body/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div[4]/div[2]/div/label[1]/span[1]"))).click()
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[2]/div/div/form/div[2]/button[2]').click()
sleep(10)
driver.quit()