参考
- https://selenium.dev/
- https://selenium-python.readthedocs.io/
- https://github.com/SeleniumHQ/selenium
环境准备
- 安装浏览器
- 安装selenium driver,并放入Path变量中 https://selenium.dev/documentation/en/getting_started_with_webdriver/browsers/
- 安装selenium-client
- 安装selenium ide
ChromeDriver
#把这一句放到环境变量里
export PATH=$PATH:/Users/seveniruby/temp/11/selenium
seveniruby:selenium seveniruby$ chromedriver
Starting ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945@{#614}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
#windows
setx /m path "%path%;C:\\WebDriver\\bin\\"
#mac linux
export PATH=$PATH:/opt/WebDriver/bin >> ~/.profile
环境变量的常见的坑
- 变量设置后,不会立马生效
- 重启你的pycharm进程,可以生效。但是也可能不会生效,不生效的场景主要是来自于windows,windows有个特殊的机制,如果不是所有的cmd进程都关闭,你的变量是不生效的。
- 重启机器
- 检查你的PATH变量设置,估计写错了。
第11期——Selenium IDE⽤例录制
第11期——Selenium测试⽤例编写
第11期——Selenium测试⽤例编写
第11期——元素定位⽅法 (ID/Name/CSS/XPath)
def find_element(self, by=By.ID, value=None):
"""
Find an element given a By strategy and locator. Prefer the find_element_by_* methods when
possible.
:Usage:
element = driver.find_element(By.ID, 'foo')
:rtype: WebElement
"""
if self.w3c:
if by == By.ID:
by = By.CSS_SELECTOR
value = '[id="%s"]' % value
elif by == By.TAG_NAME:
by = By.CSS_SELECTOR
elif by == By.CLASS_NAME:
by = By.CSS_SELECTOR
value = ".%s" % value
elif by == By.NAME:
by = By.CSS_SELECTOR
value = '[name="%s"]' % value
return self.execute(Command.FIND_ELEMENT, {
'using': by,
'value': value})['value']
class By(object):
"""
Set of supported locator strategies.
"""
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
css定位符
- document.querySelectorAll(‘.active’)
- $(‘.active’)
- $x(‘//*[@data-name=“霍格沃兹测试学院”]’)
常见错误
- selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
- not found
第11期——隐式等待与显式等待
def until(self, method, message=''):
"""Calls the method provided with the driver as an argument until the \\
return value is not False."""
screen = None
stacktrace = None
end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if value:
return value
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message, screen, stacktrace)
- title出现
- dom出现 presence
- css出现 visibility
- js执行 clickable
应用场景:
- 隐式等待尽量默认都加上,时间限定在3-6s,不要太长,这是为了所有的findElemen方法都有一个很好的缓冲
- 显式等待用来处理隐式等待无法解决的一些问题。比如 文件上传
- 死等一般不推荐
第11期——点击输⼊滑动等常⻅交互
第11期——frames与多窗⼝处理
第11期——Selenium多浏览器处理 (Chrome/Firefox/IE)
browser = os.getenv("browser").lower()
print(browser)
if browser == "headless":
self.driver = webdriver.PhantomJS()
elif browser == "firefox":
self.driver = webdriver.Firefox()
else:
self.driver = webdriver.Chrome()
self.driver.get("https://testerhome.com/")
browser=firefox pytest test_selenium/test_hogwarts.py
第11期——执⾏JavaScript脚本
第11期——⽂件上传弹框处理
课间作业1
进入testerhome,访问社团,访问霍格沃兹测试学院,访问最顶部的第一个帖子。把代码贴到回复里。
课间作业2
进入testerhome,访问MTSC2020置顶帖,点击目录,点击议题征集范围。把代码贴到回复里。
课后作业3
- 企业微信自动添加成员,需要复用已经登录的chrome,需要debugger address,代码贴到回复里