Reputation: 3
(this is my first ever selenium script and first ever stack post, not fully aware how to make this decently :P)
I'm trying to insert a variable (number) into something which doesn't appear to be a form object (instead a body).
When editing using inspect element, I can edit the body's html and it will allow me to post this edited html as a new forum post. So that could be a potential method of inserting the variable into the html.
Youtube example of code working: https://www.youtube.com/watch?v=_6VqrMOMBeI
Youtube example of editing body code manually and posting https://youtu.be/YrT88IV-obg:
Website link (if you want to take a look) https://camelotkingdom.com
I've tried doing research on stack to see if I could find other examples of people fixing this issue.
This is the main thread I've found: Modify innerHTML using Selenium
Python Code:
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()
Website body code
<body contenteditable="true" dir="LTR" style="overflow-y: hidden; min-height: 99px;"><p><br></p></body>
Full code: view-source:http://camelot.treasuremc.net/threads/count.22/
I expect the code to be posting the variable to the forum thread.
Upvotes: 0
Views: 87
Reputation: 14145
Can you please try this below piece of code.
iframe = driver.find_elements_by_tag_name("iframe")[0]
driver.switch_to.frame(iframe)
driver.execute_script("document.body.innerHTML = '<p>test</p>'")
Upvotes: 1