2017-07-19 46 views
3

我想填充文本框,但我无法找到元素。Python与硒:无法找到元素错误

我曾尝试按名称按

  • 类查找元素

    • 添加10秒等待

    但我仍然得到同样的错误。

    这里是HTML

    <div class="innerWrap"> 
        <textarea aria-autocomplete="list" aria-controls="typeahead_list_u_0_j" 
        aria-expanded="false" aria-haspopup="true" aria-label= 
        "Write a birthday wish on his Timeline..." class= 
        "enter_submit uiTextareaNoResize uiTextareaAutogrow uiStreamInlineTextarea inlineReplyTextArea mentionsTextarea textInput" 
        cols="48" id="u_0_k" name="message_text" placeholder= 
        "Write a birthday wish on his Timeline..." role="textbox" title= 
        "Write a birthday wish on his Timeline..."></textarea> 
    </div> 
    

    我的代码:

    textbox = driver.find_element_by_name('message_text') 
    textbox.send_keys('Happy Birthday! ') 
    

    的网页,我的工作是Facebook Birthdays page

  • 回答

    2

    所以,我终于能够找到该元素,通过查看页面源代码解决上述错误。

    print(driver.page_source) 
    

    页源显示元素的名称是message和不message_text。但浏览器中的检查元素仍显示为message_text

    3

    这个问题已经回答过了,但DOC答案指向的链接已过时,因此这里是新的更新"switch to"/switch commands selenium docs链接

    您需要使用下面的命令切换你的驱动程序,以便它在正确的窗口/帧/等

    检查该窗口是通过运行开放运行:首先运行driver.getWindowHandles();返回一组所有窗户

    然后使用driver.switchTo().window(handle);} #if switching to another window

    driver.switchTo().frame("yourframename");} #if switching to a frame

    driver.switchTo().alert(); #if switching to a popup

    希望这有助于

    A similar question

    +0

    我最终成功地找到了元素,但通过一种不同的方法。我试着改变窗口和框架,但没有帮助,因为我已经在同一个窗口中,代码中没有iframe。所以我通过'driver.page_source'下载了页面源,我想在下载源中定位的元素名称实际上是“消息”,而不是“message_text”。但是,当我通过检查元素在浏览器中检查此元素的名称时,它仍显示“message_text”。我不知道为什么有元素名称的差异。 –

    +1

    @AbdulMoizFarooq这是一个有趣的发现。您应该将其作为帮助其他人的答案发布。感谢更新。 –