gpt4 book ai didi

Python Selenium chrome - 如何禁用用户输入

转载 作者:行者123 更新时间:2023-11-30 22:09:55 24 4
gpt4 key购买 nike

下面是一个简单的代码,它打开一个 google chrome 实例,然后输入文本“这是一些非常长的文本”。

在 google chrome 中输入上述文本时,有没有办法禁用用户键盘输入?目标是用户在计算机上并行处理其他事情时不会在该页面上错误地输入任何内容。

我知道一种选择是采用“ headless ”chrome 实例,但这对于我想要处理的网站来说不是一个选择。

到目前为止我做了什么:

我查看了 Selenium 文档 http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.chrome.options但我找不到任何这样的选项。

但话又说回来,上述网站没有有关“--disable-notifications”的信息,这是您可以与 selenium chrome 一起使用的信息。因此,也许有可能存在一些隐藏的方法来完成我正在寻找的事情。

还检查了https://peter.sh/experiments/chromium-command-line-switches/但运气不好

import time, datetime, sys, os
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options


CHROME_PATH = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
CHROMEDRIVER_PATH = '..\\chromedriver.exe' #location of your chromedriver.exe
WINDOW_SIZE = "1920,1080"

chrome_options = Options()
#chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument("disable-gpu")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--disable-notifications")
chrome_options.binary_location = CHROME_PATH

browser = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,chrome_options=chrome_options)
browser.get("https://www.google.com")

elements = browser.find_elements_by_css_selector('#lst-ib')
if not elements:
print("NO SUCH ELEMENT FOUND!")
print("Fatal Error:Please perform all tasks manually")
else:
item1 = browser.find_element_by_css_selector('#lst-ib')
item1.send_keys('This is some very long text'+Keys.TAB)

或者如果有人能指出我应该进行研究的方向,那就太好了!谢谢

最佳答案

我想这就是你想要的`

import time, datetime, sys, os
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

CHROME_PATH = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
CHROMEDRIVER_PATH = 'C:\\python27\\chromedriver.exe' # location of your
chromedriver.exe
WINDOW_SIZE = "1920,1080"

chrome_options = Options()
# chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument("disable-gpu")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--disable-notifications")
chrome_options.binary_location = CHROME_PATH

browser = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,
chrome_options=chrome_options)
browser.get("https://www.google.com")

elements = browser.find_elements_by_css_selector('#lst-ib')
if not elements:
print("NO SUCH ELEMENT FOUND!")
print("Fatal Error:Please perform all tasks manually")
else:
browser.execute_script('document.getElementById("lst-ib").onkeypress=function()
{return false;}')
browser.execute_script('document.getElementById("lst-ib").value = "this is some
text"')

关于Python Selenium chrome - 如何禁用用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51802268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com