0

(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.

zybe
  • 3
  • 2
  • [How to Ask](https://stackoverflow.com/help/how-to-ask) is a pretty good resource to learn how to write a good Stack post. Though you've already got the basics: You're telling us what you want to do, showing a minimal snippet. The only issues I have are that you're not saying what's really happening (in contrast with what you want to happen), and that you don't explain why that Q/A you linked doesn't work for you. – Nic Jul 11 '19 at 00:38
  • This part is the only relevant piece of code. To clarify: What is happening is that I'm finding the relevant snippet and then trying to edit "test" into it to see if it will work. Unsure why it hasn't worked, no errors and I'm rather new to this :> – zybe Jul 11 '19 at 00:54
  • It's the only relevant piece of code _as far as you know_, yes. And you, like the rest of us, can make mistakes. Please [edit] your question to include a [mcve]. If you're lucky, you'll even find the bug in the process and be able to [self-answer](https://stackoverflow.com/help/self-answer). – Nic Jul 11 '19 at 01:13
  • Edited, still no fix after messing around with it for a while longer. – zybe Jul 11 '19 at 01:21
  • `execute_script` is not a method of `WebElement` (variable `editable` in your code), it is a method of `WebDriver` (variable `main_browser` in your code). Are you sure you are not getting any errors? – Kamal Jul 11 '19 at 01:43
  • Fixed that part, now replaces the entire page with "test" instead of editing that specific element. @Kamal – zybe Jul 11 '19 at 01:47
  • @Billy, Yes, `document.body` refers to your entire page. If you want to modify a single element then you need to use command as shown [here](https://stackoverflow.com/a/35926114/5319738) which you found. – Kamal Jul 11 '19 at 01:57
  • @Kamal ok, managed to get it to write

    test>

    , the issue now is, the actual input is inside of an iframe. see here: https://i.imgur.com/BDLkMza.png, how would I make it add this into the iframe document rather than the main document? (python: https://i.imgur.com/6W1ULKe.png)
    – zybe Jul 11 '19 at 02:08
  • If I understand correctly, you are trying to insert a `paragraph` into iframe document, correct? If yes, do you have more than 1 iframe in that page? – supputuri Jul 11 '19 at 02:21
  • @supputuri Yep, this is the only iframe on the page :p – zybe Jul 11 '19 at 02:29

1 Answers1

1

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>'")
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Took slight modification (replacing 'driver' with my driver variable) but worked! Thank you <3 – zybe Jul 11 '19 at 02:45
  • how would I exit that frame again? – zybe Jul 11 '19 at 02:52
  • `driver.switch_to.default_content()` will bring you back to the main window. – supputuri Jul 11 '19 at 02:54
  • I feel the right way to add the paragraph is `driver.execute_script("var paragraph = document.createElement('p'); paragraph.textContent = arguments[1]; arguments[0].appendChild(paragraph);", driver.find_element_by_tag_name('body'),comment)` after switching to the iframe. – supputuri Jul 11 '19 at 02:56