Python 使用selenium更改语言键入方向
我正在尝试使用selenium Python将文本的键入方向改为从右向左 键盘上的正常按键顺序是CTRL+SHIFT(都是右键) 我尝试了以下方法:Python 使用selenium更改语言键入方向,python,selenium,selenium-webdriver,webdriver,Python,Selenium,Selenium Webdriver,Webdriver,我正在尝试使用selenium Python将文本的键入方向改为从右向左 键盘上的正常按键顺序是CTRL+SHIFT(都是右键) 我尝试了以下方法: from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
def testLangDirChange(self):
self.driver.get("http://unixpapa.com/js/testkey.html")
XPathTB='//textarea[@name="t"]'
WebDriverWait(self.driver, 15).until(EC.presence_of_element_located((By.XPATH, XPathTB)))
TB=self.driver.find_element_by_xpath(XPathTB)
TB.click()
actionChains=ActionChains(self.driver)
actionChains.key_down(Keys.CONTROL).key_down(Keys.SHIFT).key_up(Keys.CONTROL).key_up(Keys.SHIFT).perform()
SHIFT = '\ue008'
LEFT_SHIFT = SHIFT
我在测试仪中看到了正确的按键顺序,但键入方向没有改变(我仍然从左向右)
我还尝试:
firefoxProfile.native_events_enabled = False
firefoxProfile.set_preference("intl.accept_languages", 'he-IL')
但这没有帮助
(您必须具有从右到左的键盘布局,如希伯来语,才能测试此功能)
更新1:
我刚刚在测试站点上启用了以下复选框:modifiers、DOM 3、old DOM 3,并比较了这两个输出。我看到的是,在selenium输入中,它是location=1,在键盘测试中,它是location=2
当我执行Keys.SHIFT时,Selenium可能会键入LEFT SHIFT(尽管Keys.SHIFT中单独指定了key LEFT SHIFT)
更新2:
我在模块selenium.webdriver.common.keys.keys中发现了以下内容:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
def testLangDirChange(self):
self.driver.get("http://unixpapa.com/js/testkey.html")
XPathTB='//textarea[@name="t"]'
WebDriverWait(self.driver, 15).until(EC.presence_of_element_located((By.XPATH, XPathTB)))
TB=self.driver.find_element_by_xpath(XPathTB)
TB.click()
actionChains=ActionChains(self.driver)
actionChains.key_down(Keys.CONTROL).key_down(Keys.SHIFT).key_up(Keys.CONTROL).key_up(Keys.SHIFT).perform()
SHIFT = '\ue008'
LEFT_SHIFT = SHIFT
因此,它们的定义确实是相同的。如何在其中指定右移?我不确定您的WebDriver是否可以将命令发送到操作系统级别,我认为它所做的只是在浏览器中操作。相反,为什么不直接用sendKeys(otherLang)输入另一种语言呢?我最后使用以下命令运行了一个AutoIt脚本: 系统(r''C:\path\changeLangDir.exe') 内容如下:
send("{RCTRL}{RSHIFT}")
另一个解决方案是将dir=“ltr”标记设置为dir=“rtl”
使用:
self.driver.execute_script("arguments[arguments.length-1].dir = 'rtl';", TB)
经过大量的搜索,我找到了改变chromedriver语言和使用python的解决方案。我用的是linux,这个妈妈为你工作
options = Options()
options.binary_location = "/Applications/Google\
Chrome.app/Contents/MacOS/Google\ Chrome"
options = Options()
options.add_argument("start-maximized")
prefs = {
"translate_whitelists": {"fr": "en", "de": "en", 'it': 'en', 'no': 'en', 'es':
'en', 'sv': 'en', 'nl': 'en',
'da': 'en', 'pl': 'en', 'fi': 'en', 'cs': 'en'},
"translate": {"enabled": "true"}
}
options.add_experimental_option("prefs", prefs)
如果有任何疑问,请告诉我。欢迎光临!当你发布答案时,试着解释一下。如果有以前的答案,那么展示你的优点。