首页 > 解决方案 > selenium 打开浏览器控制台并使用 python 粘贴一些 javascript 代码

问题描述

我有一个javascript,我想在页面加载时将其放入控制台,但我无法在python中使用selenium打开控制台我可以使用它访问devtoos--auto-open-devtools-for-tabs但无法打开控制台。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
import os
import time
from urllib.parse import quote_plus

driverPath = "chromedriver.exe"
dataPath = "whatsapp-assistant-bot-master/Data"

options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=" + dataPath)
options.add_argument("--auto-open-console-for-tabs=" + dataPath)
driver = webdriver.Chrome(options=options, executable_path=driverPath)

driver.get('http://google.com')
print('Please Scan the QR Code and press enter')

 driver.find_element_by_id("gsr").send_keys(Keys.F12)



input()
driver.quit()

标签: pythonseleniumselenium-chromedriver

解决方案


回答到这里,不用打开控制台,可以通过selenium运行你的js代码

使用 Python 在 Selenium 中运行 javascript


推荐阅读