无法在selenium中选择文本框

时间:2016-08-04 03:54:16

标签: python selenium selenium-webdriver

我正在尝试访问通用huffington post artical中的评论文本框。当我右键单击inspect元素时,我得到以下HTML代码:

<div class="UFIInputContainer">
 <div class="_1cb _5yk1">
  <div class="_5yk2" tabindex="-2">
   <div class="_5rp7">

突出显示行<div class="_1cb _5yk1">

from selenium import webdriver
driver = webdriver.Chrome()
'''
Just pretend that I put in some code to log in to facebook
so I can actually post a comment on huffington post
'''
driver.get.('http://www.huffingtonpost.com/entry/worst-suicide-squad-reviews_us_57a1e213e4b0693164c34744?')
'''
Just a random artical about a movie
'''
comment_box = driver.find_element_by_css_selector('._1cb._5yk1')
'''
since this is a compound class I think I should use find_by_css_selector
'''

当我运行这个时,我收到错误消息:“找不到这样的元素”。我尝试了其他尝试获取注释文本框的方法,但是我得到了相同的错误消息,而我却迷失了如何访问它。我希望有人可以解决这个问题。

编辑:这是一个更完整的HTML代码:

<html lang="en" id="facebook" class="svg ">
 <head>...</head>
  <body dir="ltr" class="plugin chrome webkit win x1 Locale_en_US">
   <div class="_li">
    <div class="pluginSkinLight pluginFontHelvetica">
     <div id="u_0_0">
      <div data-reactroot class="_56q9">
       <div class="_2pi8">
        <div class="_491z clearfix">...</div>
         <div spacing="medium" class="_4uyl _1zz8 _2392 clearfix" direction="left">
          <div class="_ohe lfloat">...</div>
          <div class>
           <div class="UFIInputContainer">
            <div class="_1cb _5yk1">
             <div class="_5yk2" tabindex="-2">
              <div class="_5rp7">
             </div>
            </div>
            <div class="UFICommentAttachmentButtons clearfix">...</div>
            <!-- react-empty: 39 -->
            <div class="_4uym">...</div>
           </div>
          </div>
         </div>
         ::after

1 个答案:

答案 0 :(得分:0)

您必须切换到包含文本框的iframe。尝试以下方法,它应该工作: 如果显示加载注释按钮,则可能首先需要单击加载注释按钮

load_comment = driver.find_element_by_css_selector('.comment-button.js-comment-button')
load_comment.click()
driver.switch_to_frame(driver.find_element_by_css_selector('.fb_ltr.fb_iframe_widget_lift'))
comment_box = driver.find_element_by_css_selector('._1cb._5yk1')
comment_box.send_keys('Test')