Home > Back-end >  How to insert text within br tag using Selenium
How to insert text within br tag using Selenium

Time:04-03

Im creating a twitter bot to make comments for me on twitter. I've got it to login and everything but to make a comment the attribute for the text box is a <br>.

This is the html attribute for the comment field <br data-text="true">

this is the full HTML for the <div> that the <br> is in

<div  style="max-height: 720px; min-height: 24px;">
    <div >
        <div >
            <div  id="placeholder-227pg" style="white-space: pre-wrap;">Tweet your reply
            </div>
        </div>
        <div >
            <div aria-activedescendant="typeaheadFocus-0.9783065535277462" aria-autocomplete="list" aria-controls="typeaheadDropdownWrapped-0" aria-describedby="placeholder-227pg" aria-label="Tweet text" aria-multiline="true"  contenteditable="true" data-testid="tweetTextarea_0" role="textbox" spellcheck="true" tabindex="0" no-focuscontainer-refocus="true" style="outline: none; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;">
                <div data-contents="true">
                    <div  data-block="true" data-editor="227pg" data-offset-key="1d4tg-0-0">
                        <div data-offset-key="1d4tg-0-0" >
                            <span data-offset-key="1d4tg-0-0">
                                <br data-text="true">
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

I've tried coping the Xpath but that didn't work. Also copying xpath for me hasn't been working when trying to login so I had to find element by name but I still don't know how to do this. I also couldn't find any information online.

CodePudding user response:

you could use send_keys in selenium to comment

comment_box = driver.find_element(
    By.XPATH, "//div[@class='public-DraftStyleDefault-block public-DraftStyleDefault-ltr']")
comment_box.click()
comment_box.send_keys('YOUR_COMMENT_HERE')

#Then post the comment by pressing reply button
driver.find_element(By.XPATH, "//span[contains(text(), 'Reply')]").click()

NOTE: You need to import the following

from selenium.webdriver.common.by import By

CodePudding user response:

The <br> tag is having the parent <div> with attribute contenteditable="true".

<div aria-activedescendant="typeaheadFocus-0.9783065535277462" aria-autocomplete="list" aria-controls="typeaheadDropdownWrapped-0" aria-describedby="placeholder-227pg" aria-label="Tweet text" aria-multiline="true"  contenteditable="true" data-testid="tweetTextarea_0" role="textbox" spellcheck="true" tabindex="0" no-focuscontainer-refocus="true" style="outline: none; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;">

Solution

To tweet the text i.e. send the character sequence to the <div> element you can use either of the following locator strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "div.notranslate.public-DraftEditor-content[aria-label='Tweet text']").send_keys("AnonyomailDeveloper")
    
  • Using xpath:

    driver.find_element(By.XPATH, "//div[@class='notranslate public-DraftEditor-content' and @aria-label='Tweet text']").send_keys("AnonyomailDeveloper")
    
  • Related