selenium内嵌在iframe中的元素定位以及select下拉框定位

新手菜鸟进行UI自动化中,持续几天,定位元素一直报错:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="slywlb"]"}

搜了各种方法,都无法定位,今天看到某博主写的文章,恍然大悟!(博主原文,值得收藏https://baijiahao.baidu.com/s?id=1712049754800680732&wfr=spider&for=pc

如果一个页面中无法定位元素,我们可以考虑,这个元素是否嵌入到了其他iframe中呢?

  1. 首先F12找到需要定位元素的根iframe

  1. 然后使用switch_to.frame移动到在指定iframe中进行定位

#某个功能内嵌在mainFrame_1126的iframe中,所以先用switch_to.frame在指定iframe中定位

driver.switch_to.frame("mainFrame_1126")

time.sleep(2)

3.select定位,使用select_by_value方法进行下拉选择

sel=driver.find_element(By.CLASS_NAME,"yw_bl_select")#使用click报错selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable,意思是元素不可交互。不能使用点击方法

#把定位的下拉菜单元素存储到Select类中

Select(sel).select_by_value("20")

select定位可参考博文https://zhuanlan.zhihu.com/p/574132001

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值