GVKun编程网logo

seleniumWebdriver:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互

8

如果您对seleniumWebdriver:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互感兴趣,那么这篇文章一定是

如果您对seleniumWebdriver:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解seleniumWebdriver:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互的各种细节,此外还有关于ElementNotVisibleException:Selenium Python、ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误、FireFox中的Selenium OpenQA.Selenium.DriverServiceNotFoundException、java+selenium报异常org.openqa.selenium.StaleElementReferenceException的解决方案的实用技巧。

本文目录一览:

seleniumWebdriver:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互

seleniumWebdriver:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互

我正在尝试单击具有文本克隆概念的范围。以下是html

<ul>    <li>    <li>    <li>    <li>    <li>        <span>Clone concept</span>    </li>    <li>    <li></ul>

我使用的javascript代码是:

driver.findElement(By.xpath("//span[text()=''Clone concept'']")).click();

我确认这是通过Firepath的元素的正确选择。

我还确保按照链接可见该元素。如何强制SeleniumWebDriver单击当前不可见的元素?

这是 计算的CSS

font-family Verdana,?Arial,?Helvetica,?sans-serif    .context-menu-list  Verdana,?Arial,?Helvetica,?sans-serif       jquery...enu.css (line 15)    body    Arial,?Helvetica,?sans-serif        swa.css (line 3)    font-size   11px    .context-menu-list  11px        jquery...enu.css (line 15)    list-style-type none    .context-menu-list  none        jquery...enu.css (line 15)

还尝试了以下代码:

WebElement foo = driver.findElement(By.xpath("//span[text()=''Clone concept'']"));Actions bar = new Actions(driver);bar.click(foo).perform();

异常:
org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与命令持续时间或超时进行交互:30.04秒构建信息:版本:‘2.24.1’,修订版:‘17205’,时间:‘2012
-06-19 16:53:24’系统信息:os.name:’Windows
7’,os.arch:’amd64’,os.version:‘6.1’,java.version:‘1.7.0’驱动程序信息:
driver.version:RemoteWebDriver

任何帮助将不胜感激。

对于那些被困在这里的人来说,另一个技巧是:
暂时,我已经能够通过将这个巨大的测试用例拆分为更简单的测试用例来向前迈进。

答案1

小编典典

不幸的是,Webdriver似乎并不擅长处理问题中所述的情况。不过,您有两种选择。使用JavaScript模拟点击:

JavascriptLibrary jsLib = new JavascriptLibrary(); jsLib.callEmbeddedSelenium(selenium,"triggerMouseEventAt", elementToClick,"click", "0,0");

要么

((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementToClick);

或者,您可以使用动作来尝试单击菜单链中的所有元素。不幸的是,我发现这是不可靠的。

我有一个脚本,可以检测菜单链中是否存在某个元素,如果按所需顺序单击该元素,最后可以单击用户想要的那个元素(如果需要),我可以将其张贴在某个地方,但是它不美观或短。

ElementNotVisibleException:Selenium Python

ElementNotVisibleException:Selenium Python

我已经检查了所有以前的类似问题,它们不适用于我的情况。

   try:      element = wait.until(      EC.presence_of_element_located((By.XPATH, "//*[@id=''_ariaId_73.folder''] | //*[@id=''_ariaId_133.folder'']"))   )   except:       print "403 : Monitoring Not Found"element.click()

它是不会进入异常块还可以,但它仍然抛出ElementNotVisibleExceptionelement.click()方法。

答案1

小编典典

关于解决方案的几句话:

  1. 与预期子句的条件presence_of_element_located()涉及 用于检查的元件是存在于页面的DOM的期望。 这并不一定意味着该元素是可见的。一旦找到元素,用于查找元素的定位器将返回WebElement。因此,我们必须更改 与之 相关的子句,presence_of_element_located()期望检查已知在页面DOM中存在的元素是否可见。 可见性意味着不仅显示元素,而且其高度和宽度都大于0。element是WebElement在可见时返回(相同)WebElementvisibility_of_element_located() __
  2. 展望未来,您已经尝试click()WebElement 调用方法。因此,代替presence_of_element_located()我们将使用该 element_to_be_clickable() 子句。
  3. 这是您自己的代码,做了一些小的更改:
    try:element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id=''_ariaId_73.folder''] | //*[@id=''_ariaId_133.folder'']")))

    except:
    print “403 : Monitoring Not Found”
    element.click()

ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误

ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误

如何解决ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误?

与文本的元素 是一个动态的元素,以便找到你要引起元素 webdriverwait元素是可点击 ,你既可以使用以下方法解决:

  • 使用CSS_SELECTOR

    webdriverwait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()
    
  • 使用XPATH

    webdriverwait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, ''action-bar'')]//button[@c-button c-button--blue'' and normalize-space()=''Close'']"))).click()
    
  • :您必须添加以下导入:

        from selenium.webdriver.support.ui import webdriverwait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

解决方法

我有一个包含源代码的页面,如下面的代码。在执行操作后,在其中显示“撤消”和“关闭”按钮。我试图单击“关闭”按钮。我已经尝试了下面的所有三个代码块,但都无法正常工作。有人可以指出我做错了什么,还是建议其他尝试?

html来源:

<div><div><i></i><div>Your stuff is going to <span>place</span> is on its way.</div><div><button> Undo</button></div><div><button> Close</button></div></div></div>

代码尝试:

#driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
driver.find_element_by_css_selector(''.c-button.c-button--blue'').click()
#driver.find_element_by_link_text(''Close'').click()

错误:

---------------------------------------------------------------------------
ElementNotVisibleException                Traceback (most recent call last)
<ipython-input-15-6d570be770d7> in <module>()
      1 #driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
----> 2 driver.find_element_by_css_selector(''.c-button.c-button--blue'').click()
      3 #driver.find_element_by_link_text(''Close'').click()

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in click(self)
     78     def click(self):
     79         """Clicks the element."""
---> 80         self._execute(Command.CLICK_ELEMENT)
     81 
     82     def submit(self):

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self,command,params)
    626             params = {}
    627         params[''id''] = self._id
--> 628         return self._parent.execute(command,params)
    629 
    630     def find_element(self,by=By.ID,value=None):

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self,driver_command,params)
    318         response = self.command_executor.execute(driver_command,params)
    319         if response:
--> 320             self.error_handler.check_response(response)
    321             response[''value''] = self._unwrap_value(
    322                 response.get(''value'',None))

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self,response)
    240                 alert_text = value[''alert''].get(''text'')
    241             raise exception_class(message,screen,stacktrace,alert_text)
--> 242         raise exception_class(message,stacktrace)
    243 
    244     def _value_or_default(self,obj,key,default):

ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=72.0.3626.109)
  (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)

FireFox中的Selenium OpenQA.Selenium.DriverServiceNotFoundException

FireFox中的Selenium OpenQA.Selenium.DriverServiceNotFoundException

我正在尝试开始编写Selenium测试,并且我编写的第一个非常基本的测试因exception失败OpenQA.Selenium.DriverServiceNotFoundException

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace WebDriverDemo
{
        class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Url = "http://www.google.com";

        }
    }
}

调试器说我需要下载geckodriver.exe并将其设置在我的PATH变量上,这已经完成,但仍然会出现相同的异常。当我对进行相同的操作时ChromeDriver,效果很好。

同样,根据MDN,如果我使用的是Selenium
3.0或更高版本,则应默认启用它。我在Windows 10计算机上使用Selenium 3.0.1。

java+selenium报异常org.openqa.selenium.StaleElementReferenceException的解决方案

java+selenium报异常org.openqa.selenium.StaleElementReferenceException的解决方案

因为页面内容有很多页,需要切换页数,但是切换跳转到第二页的时候,页面首先会自动刷新,导致出现如下异常:Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

	//获取当前页面的table内容
	static int count = 0;
	public static List getPageTableContent(WebDriver driver,WebElement table,String refreshElement ){
		
		//获取当前页面的table
//把下面这行代码的注释去掉即可
//		table= driver.findElement(By.xpath(refreshElement));
		
		List<WebElement> rows = table.findElements(By.tagName("tr"));//根据行标签,获取所有行对象
		//String[] tableTitle = {"编号","配置名称","首页返回按钮","首页banner显示","极简尾页",""};
		ArrayList<String> tableContent = new ArrayList<>();
		for(WebElement row:rows){//从所有行中遍历每一行
			List<WebElement> col = row.findElements(By.tagName("td"));//一行中,所有列标签,
			for(WebElement cell:col){//一行中,所有的列(也就是单元格)
				String content = cell.getText();//每个单元格的内容
				tableContent.add(content);
				//System.out.println(content + "...content");
				//System.out.println(driver.findElement(By.xpath("//td[contains(text(),''可以'')]")).getText() + "...findElement");
				
			}
		}
		return tableContent;
	}
	
	//获取总页数
	public static int getPageAllNo(WebDriver driver){
		//获取总页数
				String pageCountSumStr = driver.findElement(
						By.xpath("//*[@id=''product_mgt_wrap'']/div/div[2]/div[3]/table/tbody/tr/td[8]/span")).getText();
				int pageCountSum = Integer.parseInt(pageCountSumStr.substring(3));
				return pageCountSum;
	}
	
	//获取当前页数
	public static int getCurrentPageNo(WebDriver driver){
		String pageCountSumStr = driver.findElement(
				By.xpath("//*[@id=''product_mgt_wrap'']/div/div[2]/div[3]/table/tbody/tr/td[8]/span")).getText();
		//获取当前页面页数
				String pageSource = driver.getPageSource();//获取当前页的源文件
				String pageElement = "pagination-num\" type=\"text\" value=\"";//在源文件中查找含有该字段的位置
				int pageIndex = pageSource.indexOf(pageElement);//通过上面的字段位置,定位文本框中的当前页数
				//通过定位出来的位置,获取文本框中的值 
	}
	//根据表格单元个内容定位单元格所在行
	/**
	 * @author:苏一叶 email:by.su@qq.com
	 * 1.进来先获取页面的总页数
	 * 2.如果总页数大于1
	 * 3.把每一条记录所有字段和记录所在的当前页数存入json中,表头为Key,值为Value。
	 * #3.把每一条记录中除编号外的其他字段和记录所在的当前页数存入json中,编号作为Key,其他已经存入json作为Value存入HashMap中。
	 * 4.传入需要定位的字符串,根据字符在json中查找对应的页数,把所有含有该字符的记录存到
	 * @throws InterruptedException 
	 */
	public static void getRangeFromRows(WebDriver driver,String str) throws InterruptedException{
		int pageCountSum = getPageAllNo(driver);//获取总页数
		int currentPageCount = getCurrentPageNo(driver);//获取当前页数
		
		//需要定位元素的xpath
		String strContent = "//*[contains(text(),''" + str + "'')]";
		//获取当前页面的table
        String refreshElement = "//*[@id=''product_mgt_wrap'']/div[1]/div[2]/div[2]/div[2]/div[2]/table";
		WebElement table= driver.findElement(By.xpath(refreshElement));
		                                              
		if(pageCountSum == 1){
			ArrayList<String> tableContent = (ArrayList)getPageTableContent(driver,table,refreshElement);
			for(String content:tableContent){
				if(content.contains(str)){//若包含需要查找定位的关键字str
					driver.findElement(By.xpath(strContent)).click();
				}
			}
			
		}else{//页面总数大于1的时候
			boolean flag = false;//设置一个跳出的标志位
			for(int i=0;i<pageCountSum&&!flag;i++){
				//当前页面等于1的时候
				if(currentPageCount==1){
					ArrayList<String> tableContent = (ArrayList)getPageTableContent(driver,table,refreshElement);
					
					for(String content:tableContent){
						if(content.contains(str)){//若包含需要查找定位的关键字str
							driver.findElement(By.xpath(strContent)).click();
							flag = true;//若找到,即跳转出循环
							break;//退出该循环体
						}
					}
					Thread.sleep(1000);
					currentPageCount += 1;//设置页数为2,页数大于1,逻辑转到else下面的代码块
				}else{
					//点击下一页的按钮,页面跳转到下一页,从第1页跳转到第2页
					driver.findElement(By.xpath("//*[@id=''product_mgt_wrap'']/div/div[2]/div[3]/table/tbody/tr/td[10]/a/span/span[2]")).click();
					Thread.sleep(1500);
					//从第2页开始,每翻一页,都进行查找定位
					for(int n=2;n<=pageCountSum&&!flag;n++){
						ArrayList<String> tableContent = (ArrayList)getPageTableContent(driver,table);
						//点击下一页按钮
						driver.findElement(By.xpath("//*[@id=''product_mgt_wrap'']/div/div[2]/div[3]/table/tbody/tr/td[10]/a/span/span[2]")).click();
						Thread.sleep(1000);
						currentPageCount = getCurrentPageNo(driver);//获取跳转后的页数
						
						for(String content:tableContent){
							if(content.contains(str)){//若包含需要查找定位的关键字str
								driver.findElement(By.xpath(strContent)).click();
								flag = true;//找到定位跳转到flag标志位
							}
						}
						/*//获取所有页面的内容
						tableContentAll.addAll((ArrayList)getPageTableContent(driver,table,refreshElement));*/
					}
				}
			}
			Thread.sleep(1000);
		}
	}

参考自:

http://www.cnblogs.com/fengpingfan/p/4583325.html

http://stackoverflow.com/questions/28066135/org-openqa-selenium-staleelementreferenceexception-stale-element-reference-ele

今天关于seleniumWebdriver:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互的讲解已经结束,谢谢您的阅读,如果想了解更多关于ElementNotVisibleException:Selenium Python、ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误、FireFox中的Selenium OpenQA.Selenium.DriverServiceNotFoundException、java+selenium报异常org.openqa.selenium.StaleElementReferenceException的解决方案的相关知识,请在本站搜索。

本文标签: