[英]Python with Selenium: Unable to locate element error
我正在尝试填充文本框,但我无法找到元素。
我试过找到这个元素
但我仍然得到同样的错误。
这是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生日页面
这个问题之前已经得到了回答,但答案指向的文档链接已经过时,所以这里是新更新的“切换到”/切换命令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
driver.switchTo().alert(); #if switching to a popup
希望这可以帮助
所以我终于能够通过查看页面源找到该元素并修复上述错误。
print(driver.page_source)
页面源显示元素的名称是message
而不是message_text
。 但是浏览器中的inspect元素仍显示message_text
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.