java Selenium Webdriver 在表单中输入多行文本而不提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33783394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium Webdriver enter multiline text in form without submitting it
提问by rahulserver
I have a multiline text and when I am simply putting the whole text into a form using sendKeys, the form gets submitted on each line break.
我有一个多行文本,当我只是使用 sendKeys 将整个文本放入表单时,表单会在每个换行符处提交。
I tried replacing the newline with carriage return this way:
我尝试用这种方式用回车替换换行符:
String myText="Some Multiline Text....";
myText=myText.replace("\n","");
This simply removed the newlines and I could not see the newline in output text.
这只是删除了换行符,我在输出文本中看不到换行符。
Also below did not work(it also submits form at line breaks):
下面也不起作用(它还在换行符处提交表单):
String myText="Some Multiline Text....";
myText=myText.replace("\n","\r");
So how do I go about with newlines in sendkeys without submitting the form?
那么如何在不提交表单的情况下使用 sendkeys 中的换行符呢?
回答by Tamas Hegedus
This is not a Selenium issue, pressing enter in a text field often submits the form. Usually you can bypass it by using Shift+Enter to insert a new line. Try this:
这不是 Selenium 问题,在文本字段中按 Enter 键通常会提交表单。通常你可以通过使用 Shift+Enter 插入新行来绕过它。试试这个:
String myText = "first line\nsecond line";
myText = myText.replace("\n", Keys.chord(Keys.SHIFT, Keys.ENTER));
myElement.sendKeys(myText);
回答by Salih Can
You can also use the following method for selenium. I added 2 samples. Msgbox and sendkeys
您也可以对硒使用以下方法。我添加了 2 个样本。消息框和发送键
Dim myText As String = "hello\nYes"
myText = myText.Replace("\n", Environment.NewLine)
MsgBox(myText)
exDriver.FindElement(By.TagName("input")).SendKeys(myText)
Output
输出
回答by Leonardo Wolter
What worked for me using python 3 was make use of ActionChain as Tamas said and @Arount posted on Python and Selenium - Avoid submit form when send_keys() with newline
使用 python 3 对我有用的是使用 ActionChain,正如 Tamas 所说,@Arount 发布在Python 和 Selenium 上 - 避免在 send_keys() 与换行符时提交表单
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get('http://foo.bar')
inputtext = 'foo\nbar'
elem = driver.find_element_by_tag_name('div')
for part in inputtext.split('\n'):
elem.send_keys(part)
ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.ENTER).perform()