【问题标题】:Changing language typing direction using selenium使用 selenium 更改语言输入方向
【发布时间】:2015-11-26 07:45:08
【问题描述】:

我正在尝试使用 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 并比较了两个输出。我看到的是,在 selenium 输入中它是 location=1,在键盘测试中它是 location=2。

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

更新 2:

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

SHIFT        = '\ue008'
LEFT_SHIFT   = SHIFT

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

【问题讨论】:

    标签: python selenium selenium-webdriver webdriver


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我最终运行了一个 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)
      

      【讨论】:

        【解决方案3】:

        经过大量搜索后,我找到了更改 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)
        

        【讨论】:

        • 如果有任何混淆请告诉我
        • 欢迎来到 SO!当您发布答案时,请尝试解释一下。如果有以前的答案,请展示你的优点。
        猜你喜欢
        • 2015-07-19
        • 1970-01-01
        • 2014-11-15
        • 1970-01-01
        • 2016-07-15
        • 1970-01-01
        • 2015-06-12
        • 2016-05-20
        • 1970-01-01
        相关资源
        最近更新 更多