七仔的博客

七仔的博客GithubPages分博

0%

【“魔镜”系列】selenium控制浏览器

python使用selenium打开浏览器并打开指定页面&执行js语句对页面进行修改

【“魔镜”系列】selenium控制浏览器

python使用selenium打开浏览器并打开指定页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

executable_path = '浏览器驱动文件位置'
driver_url = '需要打开的网址'

option = Options()
# option.add_argument('disable_infobars') # 与下面一行效果相同(去除浏览器出现的受控制提示),新版本已失效
option.add_experimental_option('excludeSwitches', ['enable-automation'])
global driver, executable_path
driver = webdriver.Chrome(options=option, executable_path=executable_path) # 打开浏览器
driver.maximize_window() # 最大化
driver.get(driver_url) # 转到指定页面
driver.fullscreen_window() # 全屏

执行js语句对页面进行修改

1
2
3
word = '你好啊,主人'
js = 'document.getElementsByClassName("title-msg")[0].innerHTML ="' + word + '"'
driver.execute_script(js)

此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/72.html

欢迎关注我的其它发布渠道