Selenium Webdriver enter multiline text in form without submitting it

后端 未结 3 1572
走了就别回头了
走了就别回头了 2021-01-05 10:57

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.

I tried replacing the ne

相关标签:
3条回答
  • 2021-01-05 11:21

    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

    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()
    
    0 讨论(0)
  • 2021-01-05 11:22

    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:

    String myText = "first line\nsecond line";
    myText = myText.replace("\n", Keys.chord(Keys.SHIFT, Keys.ENTER));
    myElement.sendKeys(myText);
    
    0 讨论(0)
  • 2021-01-05 11:33

    You can also use the following method for selenium. I added 2 samples. Msgbox and sendkeys

    Dim myText As String = "hello\nYes"
    myText = myText.Replace("\n", Environment.NewLine)
    MsgBox(myText)
    exDriver.FindElement(By.TagName("input")).SendKeys(myText)
    

    Output

    imgur output

    0 讨论(0)
提交回复
热议问题