2017-07-19 47 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這是一個有趣的發現。您應該將其作爲幫助其他人的答案發布。感謝更新。 –