所以我尝试使用网站 https://instagram.com 练习我的 Selenium 技能
我可以使用 selenium 查找对象并单击,但无法向其发送 key 。
所以基本上,我尝试在 Instagram 上自动发表评论,我找到了“添加评论”,成功点击了它,但是当我尝试发送 key 时出现错误。
代码部分:
comment_picture = driver.find_elements_by_tag_name('textarea')
for l in comment_picture:
try:
print l.get_attribute("class")
l.click()
time.sleep(1)
l.send_keys('test')
错误部分:
预期的结果应该是我可以对 Instagram 上的每张照片发表评论。 我不想得到答案。我真的很想学习 Selenium 。如果有人知道我做错了,如果我能得到提示而不是完整的答案,那就太好了。
最佳答案
stale element
是因为点击时元素已经被修改了,你必须像这样重新找到元素
comment_picture = driver.find_elements_by_tag_name('textarea')
index = 1 # xpath index start from 1
for txt in comment_picture:
try:
txt.click()
time.sleep(1)
# re-search the textarea
txt = driver.find_element_by_xpath('(//textarea)[%s]' % index)
txt.send_keys('test')
index = index + 1
关于python - Selenium webdriver 错误 - 过时的元素引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54270119/