填写评论表并提交python&硒

时间:2015-11-05 14:53:29

标签: python forms selenium

我有一个网页,我正在尝试发布评论,但我似乎无法在评论框中显示该文字。以下是该网站的代码:

<form id="commentForm" class="comment_form" accept-charset="UTF-8">
    <p class="post-error" style="display: none;">There was a problem posting your comment, please try again.</p>
    <textarea placeholder="Leave a comment..." name="comment" id="commentBox" class="commentBox" onkeyup="limitTextReverse(jQuery('.commentBox'),jQuery('.myCount'), 140);" onkeydown="limitTextReverse(jQuery('.commentBox'),jQuery('.myCount'), 140);"></textarea>
    <span class="button grey btn-submit" class="track-click" data-track="checkin_page" data-href=":comment/post" href="#">Post<input type="submit" value="Post" /></span>
    <span class="comment-loading" style="display: none;"></span>
    <span class="counter"><abbr class="myCount">0</abbr>/140</span>
    <input type="hidden" name="checkin" value="123456789" />
  </form>

这是我到目前为止所做的:

box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "comment_form")))
box.click()

但是,当我尝试send_keys时,它会变得怪异而且没有任何内容。有关如何将文本实际放入评论框的任何想法吗?

1 个答案:

答案 0 :(得分:1)

我认为您可能正在点击form元素,但您没有点击实际的评论框。您不想输入上一级form元素,而是想输入textarea

您可以按ID选择元素,然后输入:

elem = driver.find_element_by_id("commentBox")

elem.send_keys("This is a comment I'd like to write!")

The Selenium Getting Started Guide