Python 使用selenium替换textarea中的字符串;无法通过键盘访问”;

Python 使用selenium替换textarea中的字符串;无法通过键盘访问”;,python,selenium,Python,Selenium,我想用Selenium修改textarea中的部分文本。文本区域似乎几乎是只读的 在这个使用示例algo的非常简单的示例中,如果能够在这一行上更改股票id,那就太好了: context.aapl = sid(24) 。。。例如: context.aapl = sid(39840) 。。。这是特斯拉的股票id。变量名将不再有意义,没有关系,只是一个开始 这段Selenium代码对我来说可以在不需要登录的情况下打开示例 from selenium import webdriver from se

我想用Selenium修改textarea中的部分文本。文本区域似乎几乎是只读的

在这个使用示例algo的非常简单的示例中,如果能够在这一行上更改股票id,那就太好了:

context.aapl = sid(24)
。。。例如:

context.aapl = sid(39840)
。。。这是特斯拉的股票id。变量名将不再有意义,没有关系,只是一个开始

这段Selenium代码对我来说可以在不需要登录的情况下打开示例

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

t = webdriver.Firefox()     # t stands for tab as in browser tab in my mind
t.implicitly_wait(10)
t.get('https://www.quantopian.com/algorithms/')
o = t.find_element_by_xpath("//body")       # o stands for object
o.send_keys(Keys.ESCAPE)                    # clearing the popup
o = t.find_element_by_link_text("Hello World Algorithm")
o.click()
''' for the fun of it if you want to run the backtest
o = t.find_element_by_xpath('//body')
o.send_keys(Keys.CONTROL + 'b')
o.send_keys(Keys.ESCAPE)
'''
print t.find_element_by_id('code-area').text
这是它的输出

1
# Called once at the start of the simulation.
2
def initialize(context):
3
    # Reference to the AAPL security.
4
    context.aapl = sid(24)
5
6
    # Rebalance every day, one hour and a half after market open.
7
    schedule_function(my_rebalance,
8
        date_rules.every_day(),
9
        time_rules.market_open(hours=1, minutes=30))
10
11
# This function was scheduled to run once per day at 11AM ET.
12
def my_rebalance(context, data):
13
    14
    # Take a 100% long position in AAPL. Readjusts each day to
15
    # account for price fluctuations.
16
    if data.can_trade(context.aapl):
17
        order_target_percent(context.aapl, 1.00)
那个id是“代码区”。内容包括可能存在问题的页边距编号。 下一个嵌套区域是“内部代码区”,看起来是一样的。 其次是这两个

<div class='ide-container' id='ide-container'>
<textarea class='width_100pct' id='codebox'>
尝试对其执行CTRL-A将导致此异常

>>> o = t.find_element_by_id('codebox')
>>> o.send_keys(Keys.CONTROL + 'a')
ElementNotInteractiableException:消息:键盘无法访问元素

如果文本可以被完全剪切,那么替换可以在Python中完成并粘贴,那就好了。 我并不期望Selenium能够查找和替换文本,只是惊讶地发现用户输入的可见区域禁止交互

textarea确实有自己的发现,希望不必求助于尝试将其用作解决方法

(该环境是股票市场算法的在线IDE,称为Quantopian)

这是我尝试的另一件事,没有明显的效果:

>>> t.execute_script("arguments[0].value = arguments[1]", t.find_element_by_id("ide-container"), "_new_")
欣赏任何指针。

Textarea具有
style=“display:none”
属性,这意味着您无法使用
text
属性获取其内容。在这种情况下,您可以使用:

p = t.find_element_by_id('codebox').get_attribute("textContent")
field = driver.find_element_by_css_selector('div[role="presentation"]')
driver.execute_script("arguments[0].textContent = 'New value';", field)
要将新值设置为代码字段,可以使用:

p = t.find_element_by_id('codebox').get_attribute("textContent")
field = driver.find_element_by_css_selector('div[role="presentation"]')
driver.execute_script("arguments[0].textContent = 'New value';", field)
但请注意,代码字段中的每个代码行最初显示为单独的
div
节点,具有特定的值和样式。因此,要使新值看起来与代码完全相同(格式相同),您可以准备HTML示例,例如

value = """<div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -48px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 15px; width: 21px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># Comment for new code.</span></span></pre></div>"""
Textarea具有
style=“display:none”
属性,这意味着您无法使用
text
属性获取其内容。在这种情况下,您可以使用:

p = t.find_element_by_id('codebox').get_attribute("textContent")
field = driver.find_element_by_css_selector('div[role="presentation"]')
driver.execute_script("arguments[0].textContent = 'New value';", field)
要将新值设置为代码字段,可以使用:

p = t.find_element_by_id('codebox').get_attribute("textContent")
field = driver.find_element_by_css_selector('div[role="presentation"]')
driver.execute_script("arguments[0].textContent = 'New value';", field)
但请注意,代码字段中的每个代码行最初显示为单独的
div
节点,具有特定的值和样式。因此,要使新值看起来与代码完全相同(格式相同),您可以准备HTML示例,例如

value = """<div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -48px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 15px; width: 21px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># Comment for new code.</span></span></pre></div>"""

您试图提取的带有codebox的算法内容的style属性设置为display:none。因此,要提取文本,可以使用以下代码行:

p = t.find_element_by_xpath("//div[@class='ide-container']/textarea[@id='codebox']")
t.execute_script("arguments[0].removeAttribute('style')", p)
print(t.get_attribute("innerHTML"))

您试图提取的带有codebox的算法内容的style属性设置为display:none。因此,要提取文本,可以使用以下代码行:

p = t.find_element_by_xpath("//div[@class='ide-container']/textarea[@id='codebox']")
t.execute_script("arguments[0].removeAttribute('style')", p)
print(t.get_attribute("innerHTML"))