'Is there any possibility to insert HTML tag into the file which is reside on server through selenium automation?
I have created simple automation which will first read the read subdomain folder and then it will get into it and then find for the specific html file. If it will find it (i.e: .html) then edit it and i want to add the specific tag somewhere in that file but i am unable to do it.
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")
I am attaching the picture so you would be able to see where I want to place that tag.
Solution 1:[1]
This is nodejs selenium code that works. May be you can reproduce in python. Don't forget to navigate to any page page before editing page html. Good luck.
async navigateToHomePage() {
logger.info(`inside navigateToHomePage`)
await driver.get(this.baseUrl)
}
async insertHtmlIntoDocumentBody(html) {
logger.info(`inside insertHtmlIntoDocumentBody`)
let htmlElement = driver.findElement(By.css('html'))
let headElement = driver.findElement(By.css('head'))
let bodyElement = driver.findElement(By.css('body'))
logger.info(`starting html edition`)
await driver.executeScript(`let div =
document.createElement('html');
div.innerHTML='${JSON.stringify(html)}';
arguments[0].removeChild(arguments[1]);
arguments[0].removeChild(arguments[2]);
arguments[0].appendChild(div)`, htmlElement, headElement,
bodyElement)
logger.info(`completed html edition`)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kanan |