python+selenium控制已打开页面
2019-12-16 本文已影响0人
artCoding
场景:
用户在打开网址登陆成功进入对应需要操作的页面之后执行脚本
开发环境:
python3+selenium
1.带参数启动chrome
Win:
chrome.exe --remote-debugging-port=9222 --user-data-dir="/Users/lee/Documents/selenum/AutomationProfile"
Mac:
chrome启动程序目录:/Applications/Google Chrome.app/Contents/MacOS/
进入chrome启动程序目录后执行:
./Google\ Chrome --remote-debugging-port=9222 --user-data-dir="/Users/lee/Documents/selenum/AutomationProfile"
参数说明:
--remote-debugging-port
可以指定任何打开的端口,selenium启动时要用这个端口。
--user-data-dir
指定创建新chrome配置文件的目录。它确保在单独的配置文件中启动chrome,不会污染你的默认配置文件。
2.在带参数启动的浏览器中访问对应页面
3.python脚本执行
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options
# 带参数打开chrome之后,使用脚本控制已打开页面
chrmoe_options = Options()
chrmoe_options.add_experimental_option('debuggerAddress', "127.0.0.1:9222")
driver = webdriver.Chrome(chrome_options=chrmoe_options)
# 页面操作
driver.find_elements_by_class_name('field-input')[0].send_keys("testuser")
driver.find_elements_by_class_name('field-input')[1].send_keys("testuserpwd")
time.sleep(3)
driver.find_element_by_class_name('login-btn').click()
python+selenum使用说明:https://www.jianshu.com/p/1531e12f8852
参考文章:https://blog.csdn.net/xuejianbest/article/details/86540854