首页 > 解决方案 > 是否有可能通过 selenium 自动化将 HTML 标记插入驻留在服务器上的文件中?

问题描述

我创建了简单的自动化,它将首先读取读取的子域文件夹,然后进入其中,然后查找特定的 html 文件。如果它会找到它(即:.html)然后编辑它,我想在该文件的某处添加特定标签,但我无法做到。

staticWord = "Hair"
    htmlTag = "<a href='"+ staticRandomPathList[0] + "'>" + staticWord + "</a>"
    print(htmlTag)
    # Now on we are working statically
    folderfound = 0
    filefound = 0
    for domainNameFolder in range(len(staticRandomPathList)):
        subDomainSelectedFilesAddress = driver.find_element(By.XPATH,"//table/tbody/tr[" + str(domainNameFolder + 1) + "]/td[" + str(1) + "]")
        subDomainName = new_list[domainNameFolder] + '.' + domain_list[domain_variable]
        if subDomainSelectedFilesAddress.text == "logs" or subDomainSelectedFilesAddress.text == "public_html":
            continue
        else:
            if subDomainSelectedFilesAddress.text == "test1.testlab.com":
                action = ActionChains(driver)
                action.double_click(subDomainSelectedFilesAddress).perform()
                time.sleep(1)
                for file in range(0, 10):
                    time.sleep(1)
                    selectedFile = driver.find_element(By.XPATH, "//table/tbody/tr[" + str(
                        file + 1) + "]/td[" + str(1) + "]")
                    if selectedFile.text == "5.html":
                        selectedFile.click()
                        editFile = driver.find_element(By.XPATH, "//a[@ng-click='showHTMLEditorModal()']")
                        editFile.click()


                        # addHtmlTag = WebDriverWait(driver, 20).until(
                        #     EC.visibility_of_element_located((By.CLASS_NAME, "ace_content")))
                        # insertAnchorTag = driver.find_element(By.CLASS_NAME, "ace_content")
                        # insertAnchorTag.click()
                        #
                        time.sleep(2)
                        textinput = driver.find_element(By.CLASS_NAME, "ace_text-layer")
                        print(textinput.text)

                        gettingTextFromServer = textinput.text

                        Html_file = open("HTMLParsing.html", "w")
                        newHTMLFile = Html_file.write(gettingTextFromServer)
                        html = newHTMLFile
                        print(html)
                        # soup = Soup(html)
                        # bodyTag = soup.find('body')
                        # anchor = soup.new_tag('a')
                        # anchor['href'] = staticRandomPathList[0]
                        # bodyTag.insert(anchor)


                        Html_file.close()



                        # print(insertAnchorTag.text)



                        # mapHTMLTag =  driver.find_element(By.ID, "id='htmlEditorContent'")
                        # mapHTMLTag.send_keys(htmlTag)


                        # addHtmlTag.send_keys(htmlTag)
                        filefound = 1
                        break
                    else:
                        continue
                if filefound == 1:
                    break
                folderfound = 1
                break
            else:
                continue

    print("Successfully Outside Loop")

我附上了图片,这样您就可以看到我想要放置该标签的位置。

在此处输入图像描述

标签: pythonselenium

解决方案


推荐阅读