Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium chrome-如何禁用用户输入_Python_Google Chrome_Selenium_Selenium Webdriver - Fatal编程技术网

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

Python Selenium chrome-如何禁用用户输入,python,google-chrome,selenium,selenium-webdriver,Python,Google Chrome,Selenium,Selenium Webdriver,下面是一个简单的代码,它打开一个GoogleChrome实例,然后输入文本“这是一些很长的文本” 在google chrome中键入上述文本时,有没有办法禁用用户键盘输入? 这样做的目的是,当用户在计算机上并行处理其他事情时,不能在该页面上错误地输入任何内容 我知道其中一个选择是采用chrome实例“headless”,但这不是我想要开发的网站的选择 到目前为止我所做的: 我看了一下硒文档 但我找不到这样的选择 但是,同样,上面的网站没有关于“禁用通知”的信息,这是可以与selenium chr

下面是一个简单的代码,它打开一个GoogleChrome实例,然后输入文本“这是一些很长的文本”

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

我知道其中一个选择是采用chrome实例“headless”,但这不是我想要开发的网站的选择

到目前为止我所做的:

我看了一下硒文档 但我找不到这样的选择

但是,同样,上面的网站没有关于“禁用通知”的信息,这是可以与selenium chrome一起使用的。所以,也许有一个机会,有一些隐藏的方式来做我所寻找的

也检查过了,但没有运气

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)
或者,如果有人能告诉我应该朝哪个方向做研究,那也太好了!!
谢谢

我建议您尝试一下信息亭模式选项

chrome_options = Options()
chrome_options.add_argument("--kiosk")

我想这就是你想要的`

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"')

Selenium利用用户输入方法来允许键入。我认为没有合理的方法来禁用用户输入并允许脚本模拟用户输入。如果您无法在无头模式下运行脚本,那么我建议您设置一个虚拟机来运行脚本。请查看此讨论为什么无头不是一个选项?@CoreyGoldberg,因为在无头模式下,对于我的特定页面(不是google),我被阻止说“401未经授权的访问因凭据无效而被拒绝”@Stan,这只意味着基本身份验证正在阻止您。无头不应该影响这一点。如果是这样,请使用selenium或chromedriver提交一个bug。谢谢,但这仍然不能解决问题,即当我输入某个内容时,“这是一个很长的文本”会被破坏。您需要禁用用户在“lst ib”(id)文本输入区域中的输入?如果是这样,您需要在“这是一些文本”之后阻止它们已经输入或之前,我不希望用户能够做任何互动与这个自动铬窗口。无论是之前还是之后。这是你想要的吗?在某种程度上是的……虽然理想的情况是用户无法与chrome窗口进行交互,但我认为不可能完全从客户端取消对浏览器的控制