Changing language typing direction using selenium
我正在尝试使用Selenium Python从右到左更改文本的键入方向。
键盘中正常的按键顺序为CTRL SHIFT(向右)。
我尝试了以下操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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() |
我在测试仪中看到正确的按键顺序,但是打字方向没有改变(我仍然从左到右)。
我也尝试过:
1 2 | firefoxProfile.native_events_enabled = False firefoxProfile.set_preference("intl.accept_languages", 'he-IL') |
但这没有帮助。
(您必须具有从右到左的希伯来语键盘布局才能对此进行测试)
更新1:
我刚刚在测试站点上启用了以下复选框:修饰符,DOM 3,旧DOM 3,并比较了两个输出。我看到的是在selenium输入中它是location = 1,而在键盘测试中它是location = 2。
也许当我执行Keys.SHIFT时selenium类型为LEFT SHIFT(尽管在Keys中单独指定了LEFT SHIFT键)?
更新2:
我在模块selenium.webdriver.common.keys.Keys.Keys中找到以下内容:
1 2 | SHIFT = '\\ue008' LEFT_SHIFT = SHIFT |
因此它们的定义确实相同。如何在其中指定RIGHT SHIFT?
经过大量搜索,我找到了更改chromedriver语言和使用python的解决方案。我正在使用linux.this为您工作。.
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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) |
我最终使用以下命令运行了AutoIt脚本:
os.system(r'" C:\\\\ path \\\\ changeLangDir.exe"')
,其内容为:
1 | send("{RCTRL}{RSHIFT}") |
另一种解决方案是将dir =" ltr"标记设置为dir =" rtl"
使用:
1 | self.driver.execute_script("arguments[arguments.length-1].dir = 'rtl';", TB) |
我不太确定您的WebDriver是否可以将命令发送到操作系统级别,我认为它只能在浏览器中运行。相反,为什么不尝试直接使用sendKeys(otherLang)输入其他语言呢?