新手菜鸟进行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中呢?
首先F12找到需要定位元素的根iframe

然后使用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