Replace a string in a textarea using selenium, “not reachable by keyboard”

前端 未结 2 1255
星月不相逢
星月不相逢 2021-01-14 05:01

I\'d like to modify part of the text in a textarea with Selenium. The textarea seems almost as if it were read-only.

In this very simple example using a sample algo,

2条回答
  •  没有蜡笔的小新
    2021-01-14 05:15

    Textarea has style="display: none" attribute which means that you cannot get its content with text property. In this case you can use:

    p = t.find_element_by_id('codebox').get_attribute("textContent")
    

    To set new value to code field you can use:

    field = driver.find_element_by_css_selector('div[role="presentation"]')
    driver.execute_script("arguments[0].textContent = 'New value';", field)
    

    But note that initially each code line in code field displayed as separate div node with specific value and styles. So to make new value looks exactly as code (in the same formatting) you can prepare HTML sample e.g.

    value = """
    1
    """

    and do

    driver.execute_script("arguments[0].innerHTML = arguments[1];", field, value)
    

提交回复
热议问题