搜索了几个小时后,我想知道是否有可能模拟键盘上的keydown按键.例如,我希望我的程序按住x键五秒钟,所以当我在记事本中运行时,它看起来像是这样的:xxxxxxxxxxxxx.我在互联网上尝试了不同的代码,到目前为止我能找到的最好的东西是:
import ctypes
import time
user32 = ctypes.windll.user32
inputhex = raw_input("Please enter your desired key's code (HEX): ")
keycode = int(inputhex, 16)
time.sleep(1)
#VOID keybd_event(BYTE bVk, BYTE bScan, DWORD dwFlags, PTR dwExtraInfo);
user32.keybd_event(keycode,0,2,0) #is the code for KEYDOWN
time.sleep(5)
#user32.keybd_event(keycode,0,0,0) #is the code for KEYDUP[/code]
Run Code Online (Sandbox Code Playgroud)
Sendkey模块无法解决我的问题,因为它只允许您发送单个按键而不是保持按键事件.我知道autoit,在过去使用它,但我真的想知道这是否可能与python和如何.PS我正在使用python for windows
我在使用selenium web驱动程序自动化Web应用程序时遇到了问题.
该网页有一个按钮,单击该按钮可打开一个新窗口.当我使用以下代码时,它会抛出OpenQA.Selenium.NoSuchWindowException: No window found
WebDriver.FindElement(By.Id("id of the button that opens new window")).Click();
//Switch to new window
_WebDriver.SwitchTo().Window("new window name");
//Click on button present on the newly opened window
_WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click();
Run Code Online (Sandbox Code Playgroud)
为解决上述问题,我Thread.Sleep(50000);在按钮单击和SwitchTo语句之间添加.
WebDriver.FindElement(By.Id("id of the button that opens new window")).Click();
Thread.Sleep(50000); //wait
//Switch to new window
_WebDriver.SwitchTo().Window("new window name");
//Click on button present on the newly opened window
_WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click();
Run Code Online (Sandbox Code Playgroud)
它解决了这个问题,但我不想使用该Thread.Sleep(50000); …
我想用Selenium在Python保存网页上MacOS Firefox.
到目前为止,我已设法点击COMMAND + S弹出SAVE AS window.然而,
我不知道如何:
有人可以帮忙吗?
以下是我用来点击的代码COMMAND + S:
ActionChains(browser).key_down(Keys.COMMAND).send_keys("s").key_up(Keys.COMMAND).perform()
Run Code Online (Sandbox Code Playgroud)
此外,我使用这种方法的原因是我遇到Unicode编码错误: -
写一个html文件:
file_object = open(completeName, "w")
html = browser.page_source
file_object.write(html)
file_object.close()
Run Code Online (Sandbox Code Playgroud)
写入csv文件:
csv_file_write.writerow(to_write)
Run Code Online (Sandbox Code Playgroud)
错误:
UnicodeEncodeError:'ascii'编解码器不能对位置1中的字符u'\ xf8'进行编码:序数不在范围内(128)