下面是一个简单的代码,该代码打开一个Google chrome实例,然后输入文本“这是一些很长的文本”。
在google chrome中输入上述文本时,是否有办法禁用用户键盘输入? 目的是使用户在计算机上并行执行其他操作时,不能在该页面上错误输入任何内容。
我知道一种选择是将chrome实例设置为“无头”,但这不是我要工作的网站的选择。
到目前为止我所做的:
我看了http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.chrome.options上的硒文档 但我找不到任何这样的选择。
但是再说一次,以上网站没有有关“ --disable-notifications”的信息,您可以将其与硒铬一起使用。因此,也许有一种隐藏的方式可以完成我想要的工作。
还检查了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)
或者如果有人可以指出我应该进行研究的方向,那也很棒!! 谢谢
答案 0 :(得分:3)
我认为这就是你想要的`
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"')
答案 1 :(得分:0)
我建议您一次尝试使用信息亭模式选项...
chrome_options = Options()
chrome_options.add_argument("--kiosk")