selenium 发生ElementNotInteractableException或者 ElementInvisibleException提示 元素不可交互和元素不可见(全)

自己练习测试代码如下:(该代码为练习慕课网 radio 单选框,具体页面为“慕课网”个人设置页面链接)如图:
定位具体元素
在这里插入图片描述

public void radioTest() {
		driver.get("https://www.imooc.com/user/setprofile");
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driver.findElement(By.className("js-edit-info")).click();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     //	WebElement node = driver.findElement(By.xpath("//*[@id='setting-profile']/div[1]/form/div[4]/div"));
		WebElement node = driver.findElement(By.xpath("//*[@id='profile']/div[4]/div"));
		//WebElement node = driver.findElement(By.id("profile"));
     	//*[@id="profile"]/div[4]/div    
		List<WebElement> sexRadioList = node.findElements(By.name("sex"));
//System.out.println(sexRadioList.get(1).getAttribute("value"));
		for(WebElement radio :sexRadioList){
			System.out.println(radio.getAttribute("value"));
		}
		sexRadioList.get(1).click();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driver.close();
	}

用XPath 定位父级元素 XPath路径为“@id=‘setting-profile’]/div[1]/form/div[4]/div”,
会脚本运行会提示元素无法交互异常。
再次重新获取XPath的时候,XPath路径为“//*[@id=‘profile’]/div[4]/div” ,该路径系统不会提示 元素无法交互异常。

原因可能是:自己从浏览器chroPath获得的Xpath路径 与 自动化脚本所实际定位路径不一致,导致元素无法交互。

https://www.cnblogs.com/haiya2019/p/10627736.html

2,input 标签元素不可交互(往往被覆盖)HTML< input> 内的hidden 属性被设置。
(1)临时覆盖 ------设置明确等待时间,
(2)永久覆盖-----将Driver对象进行 转化成JavaScriptExcutor实例对象,用JS代码改变 如CSS" display:none" 的状态,或者“ overflow :hidden” 的状态
https://stackoverflow.com/questions/49864965/org-openqa-selenium-elementnotinteractableexception-element-is-not-reachable-by

3 动态元素 定位,元素id属性或者XPath路径发生改变。
最好用Xpath 确定元素定位。

2019年11月28号报错记录
muke网“个人设置”页面的 编辑个人设置页面 的 职位下拉框定位。客户端脚本代码如下:

	public void downListTest() {
		driver.get("https://www.imooc.com/user/setprofile");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driver.findElement(By.className("js-edit-info")).click();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		WebElement selectJob = driver.findElement(By.id("job"));
		Select downSelect = new Select(selectJob);
		downSelect.selectByIndex(2);;
		driver.close();

	}

其中通过id(WebElement selectJob = driver.findElement(By.id(“job”));)来定位下拉框,且在浏览器控制台核查定位元素唯一性时,也是能唯一定位该元素。
但是实际通过XPath(//*[@id=“job”])定位 的元素包含id="job"的 是有两个,如图
在这里插入图片描述
可能元素定位到display=none的元素,所以提示不可交互。建议用元素层级定位或者XPath 带层级的定位,保证定位元素的唯一性。

2019年12月4号 提示元素无法定位

/**
	 * Windows多窗口的切换
	 * 
	 * @param args
	 */
	public void windowsSwitchToTest() {
		Set<String> windowsSet = driver.getWindowHandles();
		String currentWindow = driver.getWindowHandle();
		System.out.println(windowsSet);
		System.out.println(currentWindow);
		for (String handle : windowsSet) {
			if (handle.equals(currentWindow)) {
				continue;
			} else {
				System.out.println(handle);
				driver.switchTo().window(handle);
			}
		}
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
		//注意:定位前确保元素在窗口范围内,
		点击事件 且不被其他元素 遮挡。 
		本例解决方式为:将窗口滚动条下拉,将元素确保可见且不被覆盖		//driver.findElement(By.xpath("//div[contains(@class,'shizhan-course-list clearfix')]/div[1]")).click();		
		System.out.println(driver.findElement(By.xpath("/html/body/div[7]/div[2]/div/div[1]/div/div/a")));
	}

问题描述:上述代码 基于慕课网首页 进行 windows 窗口的切换练习,点击菜单栏 如图所示:
在这里插入图片描述
跳转到HTML/CSS详情页面,如图:
在这里插入图片描述
目的是定位红框所示元素:用XPath(/html/body/div[7]/div[2]/div/div[1]/div/div/a)能够确保元素定位为唯一,但是还是提示元素无法定位问题:
Exception in thread “main” org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“xpath”,“selector”:"/html/body/div[7]/div[2]/div/div[1]/div/div/a"}

分析原因:(1)定位所需的元素 可能是被上层,如图所示元素覆盖,提示无法定位
上层覆盖元素
(2)也有可能是元素过大,浏览器窗口没有伸展开。 应该是 该目的元素的父级元素过大 窗口没法完全显示。
解决手段
用代码拉动右侧滚动条,让元素显示在窗口范围内。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值