带Selenium的Python:无法定位元素错误
我试图填充一个文本框,但找不到元素 我试着找到元素带Selenium的Python:无法定位元素错误,python,html,python-3.x,selenium,Python,Html,Python 3.x,Selenium,我试图填充一个文本框,但找不到元素 我试着找到元素 按班级 名字 增加了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
- 按班级
- 名字
- 增加了10秒的等待时间
<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>
我正在处理的网页是这个问题以前已经回答过了,但答案指向的文档链接已经过时,因此这里是新更新的链接 您需要使用此命令切换驱动程序,使其在正确的窗口/帧/等中运行 通过运行:first run
driver.getWindowHandles()检查哪些窗口处于打开状态
返回一组所有窗口
然后使用driver.switchTo().window(handle);}#如果切换到其他窗口
或者driver.switchTo().frame(“yourframename”);}#如果切换到帧
或driver.switchTo().alert()#如果切换到弹出窗口
希望这有帮助
因此,我终于能够找到元素并通过查看页面源代码修复上述错误
print(driver.page_source)
页面源显示元素的名称是
message
,而不是message\u text
。但是,浏览器中的inspect元素仍显示消息\u文本
我最终成功地找到了元素,但方法不同。我尝试更改窗口和框架,但没有帮助,因为我已经在同一个窗口中,代码中没有iframe。因此,我通过driver.page\u source
下载了页面源代码,我想在下载的源代码中定位的元素名称实际上是“message”,而不是“message\u text”。但是,当我通过inspect元素在浏览器中检查此元素的名称时,它仍然显示“message_text”。我不知道为什么元素名会有差异。@AbdulMoizFarooq这是一个有趣的发现。你应该把它贴出来作为帮助别人的答案。谢谢你的更新。
print(driver.page_source)