使用硒改变语言输入方向

Changing language typing direction using selenium

我正在尝试使用 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 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()

我在测试仪中看到了正确的键序列,但打字方向没有改变(我仍然从左到右)。

我也试过:

firefoxProfile.native_events_enabled = False
firefoxProfile.set_preference("intl.accept_languages", 'he-IL')

但这并没有帮助。

(您必须具有从右到左的键盘布局,例如希伯来语才能对此进行测试)

更新 1:

我刚刚在测试站点上启用了以下复选框:修饰符、DOM 3、旧 DOM 3 并比较了两个输出。我看到的是,在硒输入中它是 location=1 而在键盘测试中它是 location=2.

也许当我这样做时,Selenium 键入了 LEFT SHIFT Keys.SHIFT(尽管 LEFT SHIFT 键在 Keys 中单独指定)?

更新 2:

我在模块 selenium.webdriver.common.keys.Keys 中发现以下内容:

SHIFT        = '\ue008'
LEFT_SHIFT   = SHIFT

所以他们确实定义相同。如何在其中指定 RIGHT SHIFT?

我不太确定您的 WebDriver 是否可以将命令发送到 OS 级别,我认为它的作用是仅在浏览器中运行。相反,您为什么不尝试使用 sendKeys(otherLang) 直接输入其他语言?

我最终 运行 一个 AutoIt 脚本使用:

os.system(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.this ma 为你工作..

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)