为了满足这个需求我可是拜访了度娘好几个小时,也找不到满意的答案,刚才问了测试的朋友怎么实现,一下子有了答案,在此总结一下
步骤
1、首先在命令行中输入以下命令:chrome.exe --remote-debugging-port=1688 --user-data-dir=“D:\TestPy”
其中端口1688为任意一个端口
这时会在D:\TestPy中生成一系列文件
(在这之前别忘了在环境变量中PATH里将chrome的路径添加进去,我的环境变量是在安装浏览器的时候自动添加的)
命令行执行完之后会自动打开浏览器窗口,这个窗口就是你接下来要操作的窗口了
2、现在可以敲代码了
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1688")
chrome_driver = "X:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, options=chrome_options)
怎么样,学会了吗?
- 额外讲一点知识
在实现这个需求的过程中我发现对input元素执行clear()清空没用,可以用以下方法:
先全选再输入,达到清空的效果
from selenium.webdriver.common.keys import Keys
titleEle = driver.find_element_by_css_selector('input[placeholder=""][value=""]')
titleEle.send_keys(Keys.CONTROL, 'a')
titleEle.send_keys('码林鼠,一天进步一点点')
再次感谢朋友的帮助