Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将文本插入到非表单对象中_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python 将文本插入到非表单对象中

Python 将文本插入到非表单对象中,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,这是我有史以来第一个selenium脚本和第一个stack post,但我并不完全知道如何恰当地实现它:p 我试图将一个变量数字插入到一个看起来不是表单对象而是实体的东西中 当使用inspect元素进行编辑时,我可以编辑主体的html,它将允许我将编辑后的html作为新的论坛帖子发布。因此,这可能是一种将变量插入html的潜在方法 Youtube代码工作示例: Youtube手动编辑正文代码并发布的示例 : 网站链接,如果你想看看 我试着对stack进行研究,看看是否能找到其他人解决这个问题

这是我有史以来第一个selenium脚本和第一个stack post,但我并不完全知道如何恰当地实现它:p

我试图将一个变量数字插入到一个看起来不是表单对象而是实体的东西中

当使用inspect元素进行编辑时,我可以编辑主体的html,它将允许我将编辑后的html作为新的论坛帖子发布。因此,这可能是一种将变量插入html的潜在方法

Youtube代码工作示例:

Youtube手动编辑正文代码并发布的示例 :

网站链接,如果你想看看

我试着对stack进行研究,看看是否能找到其他人解决这个问题的例子

这是我找到的主要线索:

Python代码:

loginpage = 'http://camelotkingdom.com/login'
# login page for the website (login code redacted)

thread = 'http://camelotkingdom.com/threads/count.22/'
# counting thread link

main_browser = webdriver.Chrome("D:\AutoForum\extras\chromedriver.exe")
# chrome driver variable

# below code collects the latest count number (based on post number)
def getlatestnumber():
    main_browser.get(thread)
    # goes to the thread page

    time.sleep(2)
    # waiting for page to fully load

    links = main_browser.find_elements_by_partial_link_text('#')
    for link in links:
        a = link.get_attribute("text")
        a = a.replace('#', '')
        a = int(a)
    # grabs the current count number

    global currentnum
    currentnum = a + 1
    # sets the next count number

    print("[DEBUG] Next count is:",currentnum)

def post_main():
    print("[DEBUG] Number posting:",currentnum)

    comment = "test"
    # text to enter into the post creator iframe

    editable = main_browser.find_element_by_css_selector("iframe")
    editable.click()
    # finding and clicking the post creator iframe

    element = main_browser.execute_script("var ele=arguments[0]; ele.innerHTML = '<p>" + comment +  "</p>';", editable);
    # editing the iframe code to include the variable in a paragraph tag.



 #main_browser.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/div/div[2]/div[4]/form/div[2]/input[2]").click()

网站主体代码

<body contenteditable="true" dir="LTR" style="overflow-y: hidden; min-height: 99px;"><p><br></p></body>
完整代码: 查看来源:


我希望代码将变量发布到论坛线程。

请尝试下面的代码

 iframe = driver.find_elements_by_tag_name("iframe")[0]
 driver.switch_to.frame(iframe)
 driver.execute_script("document.body.innerHTML = '<p>test</p>'")

是学习如何写一篇好的堆栈文章的一个很好的资源。尽管你已经掌握了基本知识:你告诉我们你想做什么,只显示了一个小片段。我唯一的问题是,你没有说真正发生了什么,而不是你想发生什么,你没有解释为什么你链接的Q/A对你不起作用。这部分是唯一相关的代码。澄清一下:我正在查找相关的代码片段,然后尝试将测试编辑到其中,看看它是否能工作。不确定为什么它不起作用,没有错误,而且我对这一点相当陌生:>据您所知,这是唯一相关的代码,是的。和我们一样,你也会犯错误。请将您的问题包括在内。如果幸运的话,您甚至会在过程中发现错误并能够进行编辑。编辑,但在与它混在一起一段时间后仍然无法修复。执行_脚本不是代码中WebElement变量可编辑的方法,而是代码中WebDriver变量main_browser的方法。你确定没有收到任何错误吗?进行了轻微的修改,用我的驱动变量替换了“driver”,但成功了!谢谢,我该如何再次退出该框架?driver.switch_to.default_内容会将您带回主窗口。我觉得添加段落的正确方法是driver.execute_scriptvar paragration=document.createElement'p';段落.textContent=参数[1];参数[0]。AppendChild段落;,驱动程序。通过标签查找元素,切换到iframe后进行注释。