0

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 class="r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1ttztb7" style="max-height: 720px; min-height: 24px;">
    <div class="DraftEditor-root">
        <div class="public-DraftEditorPlaceholder-root">
            <div class="public-DraftEditorPlaceholder-inner" id="placeholder-227pg" style="white-space: pre-wrap;">Tweet your reply
            </div>
        </div>
        <div class="DraftEditor-editorContainer">
            <div aria-activedescendant="typeaheadFocus-0.9783065535277462" aria-autocomplete="list" aria-controls="typeaheadDropdownWrapped-0" aria-describedby="placeholder-227pg" aria-label="Tweet text" aria-multiline="true" class="notranslate public-DraftEditor-content" 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 class="" data-block="true" data-editor="227pg" data-offset-key="1d4tg-0-0">
                        <div data-offset-key="1d4tg-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr">
                            <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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

1

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" class="notranslate public-DraftEditor-content" 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")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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
Monirul Islam
  • 140
  • 1
  • 5