我如何使用Python中的Selenium Driver在Facebook上发送消息

时间:2015-10-06 07:34:48

标签: python facebook selenium xpath selenium-webdriver

我正在尝试使用python中的selenium向我的FB朋友发送消息。我已经尝试过Element by Xpath,CSS Selector,Class但是它不起作用请帮助我如何使用

Message_button = browser.find_element_by_id("u_0_t")
Message_button.click()
time.sleep(10)

message = browser.find_element_by_xpath("//textarea[@class='luiTextareaAutogrow _552m']")
message.send_keys("Hi this is Mark how are you..!")
message.submit()
time.sleep(10)

这是FB Code

<div class="fbNubFlyoutFooter">
  <div class="_552h _n4k">
    <textarea style="height: 15px;" class="uiTextareaAutogrow _552m" data-ft="{&quot;tn&quot;:&quot;+N&quot;}" onkeydown="run_with(this, [&quot;legacy:control-textarea&quot;], function() {TextAreaControl.getInstance(this)});"></textarea>
  </div>
  <div style="right: 0px;" class="_552n">
    <div class="_10nr"></div>
    <div class="_6a _552o">
      <form action="https://upload.facebook.com/ajax/mercury/upload.php" class="_vzk" title="Add Photos" method="post" data-reactid=".12">
        <input name="attach_id" data-reactid=".12.0" type="hidden">
        <input name="images_only" value="true" data-reactid=".12.1" type="hidden">
        <div class="_m _4q60 _3rzn _6a" data-reactid=".12.2"><a tabindex="0" class="_4q61 _5f0v _509v" data-reactid=".12.2.0"><i class="_509w" alt="Camera" data-reactid=".12.2.0.0"></i></a>
          <input class="_n" name="attachment[]" multiple="" accept="image/*" title="Add Photos" data-reactid=".12.2.1" type="file">
        </div>
      </form>
    </div>
    <div class="uiToggle _1tn3 emoticonsPanel" data-ft="{&quot;tn&quot;:&quot;+O&quot;}"><a class="_4vui" tabindex="0" title="Choose a sticker or emoticon" rel="toggle" role="button"><i class="_4l9x"></i></a>
      <div class="_5r8f panelFlyout uiToggleFlyout">
        <div class="_5r8p">
          <div style="padding:30px;text-align:center;" data-reactid=".13"><span class="img _55ym _55yq _55yo _5d9-" aria-label="Loading..." aria-busy="true" data-reactid=".13.0"></span>
          </div>
        </div>
        <div class="panelFlyoutArrow"></div>
      </div>
    </div>
    <div class="lfloat"></div>
    <div class="_5g2o"><a tabindex="0" aria-label="Send a Like" class="_3s0d" title="Send a Like" data-reactid=".10"><i class="_2y9i _30yy" data-reactid=".10.0"></i></a>
    </div>
  </div>
</div>

请帮助我使用正确的方法

1 个答案:

答案 0 :(得分:0)

you can use css_selector and Keys.ENTER as follows:

# enter your text by using css selector
driver.find_element_by_css_selector(".uiTextareaAutogrow._552m").send_keys("Merhaba, nasilsiniz?")

# you can send your message by sending native keyboard ENTER
driver.find_element_by_css_selector(".uiTextareaAutogrow._552m").send_keys(Keys.ENTER)