Created
May 14, 2020 07:04
-
-
Save luzihang123/988602cb2af1e687139713b7f31da232 to your computer and use it in GitHub Desktop.
selenium 远端/本地 替换UA 测试代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | |
import requests | |
import logging | |
def test_chrome_server(host): | |
""" | |
主动调用,等待docker容器内chrome浏览器server启动 | |
:return: | |
""" | |
i = 0 | |
while True: | |
try: | |
resp = requests.get("{}/wd/hub/status".format(host), timeout=0.5) | |
except: | |
i += 1 | |
if i > 10: | |
raise | |
else: | |
print("selenium连接信息:{}".format(resp.json())) | |
break | |
options = webdriver.ChromeOptions() | |
# options.add_argument('user-agent=' + user_agent) | |
options.add_argument('user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"') | |
desired_capabilities = DesiredCapabilities.CHROME | |
test_chrome_server('http://192.168.95.56:4444') | |
browser = webdriver.Remote( | |
command_executor="{}/wd/hub".format('http://192.168.95.56:4444'), | |
desired_capabilities=desired_capabilities, | |
options=options | |
) | |
# browser = webdriver.Chrome( | |
# chrome_options=options, | |
# desired_capabilities=desired_capabilities, | |
# ) | |
browser.get('https://httpbin.org/get?show_env=1') | |
print(browser.page_source) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment