最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Selenium给输入框send_keys无法输入文字

selenium crifan 14086浏览 0评论

在折腾:

【记录】尝试用Python操作PhantomJS+Selenium去模拟购物操作

期间,用:

searchInputElement = driver.find_element_by_id(“ctl00_hdr_hss_searchBox”)

logging.info(“searchInputElement=%s”, searchInputElement)

toSearch = gCfg[“befrugal”][“search”]

searchInputElement.send_keys(toSearch)

调试期间,发现searchInputElement也是有值的:

对应着网页端,也没有问题,就是这个id:

但是调用send_keys去输入文字,却没有效果:

但是之前调试期间也是同样的处理,却是输入文字的。

selenium sendkeys not working

java – sendKeys() not working for one of my input fileds – Stack Overflow

貌似有人提到:需要先获取焦点,然后才能输入?

或者是调用执行js代码

期间去调试,搜ctl00_hdr_hss_searchBox发现js代码中也有:

sendkeys are not working in Selenium Webdriver – Stack Overflow

先试试click后clear,再send_keys

selenium send_keys not working

Python Selenium: input textbox, send_keys not working – Stack Overflow

Python Selenium-send_keys() function is not working – Stack Overflow

Python selenium send_keys not working – Stack Overflow

searchInputElement.click()

searchInputElement.clear()

searchInputElement.send_keys(toSearch)

结果:

【已解决】Selenium已通过ID获得元素后click出错:element is not attached to the page document

【总结】

解决了元素失效的问题后,自然send_keys也就正常了。

原因:

虽然页面地址是一样的

但是中间由于用户登录,导致页面刷新了

所以之后获取的页面元素都失效了。

解决办法:

driver去手动get一下该url

这样后续获取元素就都是有效的了。

最终代码是:

driver.get(befrugalHomeUrl)

myAccountElement = driver.find_element_by_id(“ctl00_hdr_hpi_liMyAccount”)

logging.info(“myAccountElement=%s”, myAccountElement)

screenshotAndSave(“befrugal_hasLoginMyAccount.png”)

toSearch = gCfg[“befrugal”][“search”]

searchInputElement = driver.find_element_by_id(“ctl00_hdr_hss_searchBox”)

logging.info(“searchInputElement=%s”, searchInputElement)

searchInputElement.send_keys(toSearch)

转载请注明:在路上 » 【已解决】Selenium给输入框send_keys无法输入文字

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.170 seconds, using 22.13MB memory