昨天,我将Chrome和Chromedriver更新为最新版本,然后在运行Cucumber特性时收到以下错误消息:
....
unknown error: Cannot construct KeyEvent from non-typeable key
(Session info: chrome=98.0.4758.80) (Selenium::WebDriver::Error::UnknownError)
#0 0x55e9ce6a4093 <unknown>
#1 0x55e9ce16a648 <unknown>
#2 0x55e9ce1a9866 <unknown>
#3 0x55e9ce1cbd29 <unknown>
.....我尝试用Capybara的fill_in方法填充文本字段。在调试过程中,我注意到Capybara存在问题,特别是在符号@和\方面。所有其他字符都可以写入文本字段,没有任何问题。
触发错误的代码如下所示
def sign_in(user)
visit new_sign_in_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Sign in'
enduser.email包含一个类似于"example1@mail.com"的字符串。
我使用Rails 6.1.3.1,Cucumber 5.3.0,Chromedriver 98.0.4758.48,capybara 3.35.3
此错误仅发生在使用@javascript标记的功能上。
您知道是什么导致了这个错误,或者如何修复它吗?
发布于 2022-02-03 09:23:43
现在,最简单的方法是将引脚添加到较早版本的chrome驱动程序中,因此将其添加到capybara配置中。
红宝石
# /test/support/system/capybara_config.rb
require 'webdrivers/chromedriver'
Webdrivers::Chromedriver.required_version = '97.0.4692.71'希望这个问题在将来的色度驱动程序版本中得到解决,它有被提出来并在这里讨论。
我也尝试过重写fill_in方法。
这不是理想的,实际上操作系统依赖,所以请提供更好的解决方案或更新此答案。我将努力更新我的研究进展。
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
# overriding the `fill_in` helper for filling in strings with an `@` symbol
def fill_in(locator = nil, with:, currently_with: nil, fill_options: {}, **find_options)
return super unless with.include? "@"
find_options[:with] = currently_with if currently_with
find_options[:allow_self] = true if locator.nil?
element = find(:fillable_field, locator, **find_options)
email_front, email_back = with.split("@")
element.send_keys(email_front)
page.driver.browser.action
.key_down(Selenium::WebDriver::Keys[:alt])
.send_keys('g')
.key_up(Selenium::WebDriver::Keys[:alt])
.perform
element.send_keys(email_back)
end
end发布于 2022-02-03 08:25:39
ChromeDriver的新版本似乎发生了一些变化,不再可能使用send_keys方法直接发送一些特殊的字符。
在这个链接中,您将看到它是如何解决的(在C#中)-> Selenium - SendKeys("@")写一个"à“
关于python实现,请检查这个-> https://www.geeksforgeeks.org/special-keys-in-selenium-python/。
具体来说,我的实现是(使用MAC):
driver.find_element('.email-input', 'user@mail.com')现在我不得不把它改为:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
emailParts = 'user@mail.com'.split('@')
emailElement = driver.find_element('.email-input')
emailElement.send_keys(emailParts[0])
action = ActionChains(driver)
action.key_down(Keys.ALT).send_keys('2').key_up(Keys.ALT).perform()
emailElement.send_keys(emailParts[1])发布于 2022-02-04 10:35:19
我在Python中也遇到了同样的问题,似乎只使用ActionChains就可以轻松地解决这个问题。
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
def safe_send_keys(
driver,
input_selector: str,
input_text: str,
selector_type = By.CSS_SELECTOR
):
driver.find_element(selector_type, input_selector).click()
action = ActionChains(driver)
action.send_keys(input_text)
action.perform()
# Example
driver = webdriver.Chrome()
# ... Go to your web page
email = "email_with_at@email.com"
selector = "input_selector"
safe_send_keys(driver, selector, email)https://stackoverflow.com/questions/70967207
复制相似问题