有问题可回帖
基本API
- forward
- max
- fullscreen
- cookie
- switch
- execute_script
execute_script
不要漏掉return
def test_execute_script(self):
raw=self.driver.execute_script("return JSON.stringify(window.performance.timing)")
print(raw)
结果
{
"navigationStart": 1560562650364,
"unloadEventStart": 0,
"unloadEventEnd": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1560562650374,
"domainLookupStart": 1560562650386,
"domainLookupEnd": 1560562650386,
"connectStart": 1560562650386,
"connectEnd": 1560562650560,
"secureConnectionStart": 1560562650434,
"requestStart": 1560562650561,
"responseStart": 1560562650707,
"responseEnd": 1560562650710,
"domLoading": 1560562650718,
"domInteractive": 1560562651496,
"domContentLoadedEventStart": 1560562651497,
"domContentLoadedEventEnd": 1560562651537,
"domComplete": 1560562652148,
"loadEventStart": 1560562652148,
"loadEventEnd": 1560562652151
}
这个结果比requests更详细,可以获取requests无法得到的一些内部指标。
RemoteWebDriver
- https://selenium-python.readthedocs.io/getting-started.html#using-selenium-with-remote-webdriver
- Selenium Standalone Server https://www.seleniumhq.org/download/
启动selenium server,类似appium server
java -jar selenium-server-standalone-3.141.59.jar -debug
脚本更改
#self.driver = webdriver.Chrome(options=options)
self.driver=webdriver.Remote(desired_capabilities=DesiredCapabilities.CHROME)
传统模式下 webdriver.Chrome(
python testcase → chromedriver → chrome
Remote模式下
python testcase → 远程selenium server → chromedriver/iedriver → chrome/ie
Grid模式下
python testcase → grid hub/类似STF → grid node == selenium server → chromedriver/iedriver → chrome/ie
cookie
def test_cookie(self):
print(self.driver.get_cookies())
self.driver.add_cookie({"name": "a", "value":"b"})
self.driver.add_cookie({"name": "name", "value": "name demo"})
print(self.driver.get_cookies())
document.cookie获取测试
"user_id=XXXXXXX; _ga=XXXXXXXXXXXX; _gid=XXXXX; _gat=1; a=b; name=name demo; hasSkipWelcomePage=1"
Page Object
2019-06-15 10:19:37
-
selenium python库中的文档 6. Page Objects — Selenium Python Bindings 2 documentation
-
firefox母公司开源 PyPOM https://github.com/mozilla/PyPOM/tree/master/src/pypom
-
driver的封装
-
page的封装
-
testcase
休息时间:2019-06-15 10:38:10 - 2019-06-15 11:00:00
from time import sleep
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
from web.page_object.page.MainPage import MainPage
class TestXueqiu(object):
def setup(self):
self.driver=webdriver.Remote(desired_capabilities=DesiredCapabilities.CHROME)
self.driver.implicitly_wait(10)
self.driver.get("https://xueqiu.com/")
self.main=MainPage(self.driver)
def test_search(self):
self.main.search("alibaba").follow("1688")
#todo: add assertion
def teardown(self):
sleep(60)
self.driver.quit()
常见问题
18:06:33.381 DEBUG [WebDriverServlet.handle] - Found handler: org.openqa.selenium.remote.server.commandhandler.BeginSession@3baa136a
18:06:33.389 DEBUG [WebDriverServlet.lambda$handle$0] - /session: Executing POST on /session (handler: BeginSession)
18:06:33.524 INFO [ActiveSessionFactory.apply] - Capabilities are: {
"browserName": "chrome",
"version": ""
}
18:06:33.526 INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
18:06:33.542 DEBUG [UrlChecker.waitUntilAvailable] - Waiting for [http://localhost:21090/status]
18:06:33.543 DEBUG [UrlChecker.lambda$waitUntilAvailable$1] - Polling http://localhost:21090/status
18:06:33.561 DEBUG [UrlChecker.lambda$waitUntilAvailable$1] - Polling http://localhost:21090/status
Starting ChromeDriver 75.0.3770.90 (a6dcaf7e3ec6f70a194cc25e8149475c6590e025-refs/branch-heads/3770@{#1003}) on port 21090
Onl18:06:33.582 DEBUG [UrlChecker.lambda$waitUntilAvailable$1] - Polling http://localhost:21090/status
y local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
18:06:33.626 DEBUG [UrlChecker.lambda$waitUntilAvailable$1] - Polling http://localhost:21090/status
18:06:33.708 DEBUG [UrlChecker.lambda$waitUntilAvailable$1] - Polling http://localhost:21090/status
18:06:33.874 DEBUG [UrlChecker.lambda$waitUntilAvailable$1] - Polling http://localhost:21090/status
18:06:34.198 DEBUG [UrlChecker.lambda$waitUntilAvailable$1] - Polling http://localhost:21090/status
java版本的bug问题
$(/usr/libexec/java_home -v 10)/bin/java -jar selenium-server-standalone-3.141.59.jar --debug
#1.8.x的java有个bug,在解析localhost的时候可能会报错,如果有问题请升级java到更高的版本
$(/usr/libexec/java_home -v 1.8)/bin/java -jar selenium-server-standalone-3.141.59.jar --debug