NoSuchElementException, Selenium unable to locate element



  • i would like to find my TextField in selenium, but i dont know how (i use sellenium for the first time).

    I tried:

     driver.findElement(By.id(\"originTextField\"))
    
    

    or by xPath and cssSelector String generated by chrome in dev tools.

    Please help me, i would appreciate explanation.

    this is html:



  • NoSuchElementException

    org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.

    NoSuchElementException can be thrown in 2 cases as follows :

    • When using WebDriver.findElement(By by) :

      //example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
      
      
    • When using WebElement.findElement(By by) :

      //example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
      
      

    As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :

    Constant Field      Type                                        Value
    SESSION_ID          public static final java.lang.String        "Session ID"
    e.g. (Session info: chrome=63.0.3239.108)
    
    DRIVER_INFO         public static final java.lang.String        "Driver info"
    e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)
    
    BASE_SUPPORT_URL    protected static final java.lang.String     "http://seleniumhq.org/exceptions/"
    e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)
    
    

    Reason

    The reason for NoSuchElementException can be either of the following :

    • The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
    • The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
    • The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
    • The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.
    • The WebElement you are trying to locate is within an <iframe> tag.
    • The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.

    Solution

    The solution to address NoSuchElementException can be either of the following :

    • Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.

      Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?

    • Use executeScript() method to scroll the element in to view as follows :

      WebElement elem = driver.findElement(By.xpath("element_xpath"));
      ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
      
      

      Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium

    • Incase element is having the attribute style="display: none;", remove the attribute through executeScript() method as follows :

      WebElement element = driver.findElement(By.xpath("element_xpath"));
      ((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element)
      element.sendKeys("text_to_send");
      
      
    • To check if the element is within an <iframe> traverse up the HTML to locate the respective <iframe> tag and switchTo() the desired iframe through either of the following methods :

      driver.switchTo().frame("frame_name");
      driver.switchTo().frame("frame_id");
      driver.switchTo().frame(1); // 1 represents frame index
      
      

      Here you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.

    • If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :

      • To wait for presenceOfElementLocated :

        new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
        
        
      • To wait for visibilityOfElementLocated :

        new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
        
        
      • To wait for elementToBeClickable :

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
        
        


  • Your code is correct, I suspect the issue caused the page not complete load when you find the element.

    Try add a long sleep before find element, if adding sleep worked, change sleep to wait.

    Here is the code, It means waiting 10s if the element isn’t present:

    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "originTextField"))
    )
    
    


最新内容

  • title: 码云上部署hexo博客框架

    Hexo框架在码云上实现个人博客 本文受 https://www.jianshu.com/p/84ae2ba1c133 启发编写 本地调试 安装完Node.js https://nodejs.org/en/ 安装git bash工具 https://gitforwindows.org/ 在目录下建立博客文件夹,同时cd进目录 右键 git bash here 输入命令 npm install -g hexo-cli 查看是否安装成功,输入命令 hexo -v 出现版本号即为安装成功 搭建开始

    可以参考官方文档 https://hexo.io/zh-cn/docs/

    创建架构 hexo init

    如果无法完成这一步的就去显示clon后的那个github网址去下载下来解压,效果是一样的,同时注意那个主题里面是空的,需要去主题网站下载一个主题放里面。
    2. 开始本地调试

    hexo s(这一步主要是看本地效果,以后也是在这里觉得差不多了再往gitee上部署。 在浏览器中输入http://localhost:4000 看效果 在git bash窗口中按 ctrl + c 退出本地服务 创建博文 hexo generate(或直接输入hexo g)目录中就会多出一个public文件夹 hexo new "我的第一篇博文" 文章会自动生成在/source/_post目录下 Hexo 提供了快速方便的一键部署功能,但是我们要先安装一个插件。

    在git bash输入命令:

    npm install hexo-deployer-git --save 接下来是配置根目录_config.jml文件修改deploy的值,修改前如下:

    deploy:

    type: git

    repo: 你的仓库地址

    远端部署 登陆Gitee并新建仓库 创建好了后留意你仓库的那个地址 开始部署 在blog文件夹中打开git bash输入命令: git config --global user.name "Your Name" git config --global user.email "email@example.com"

    hexo deploy

    之后会弹出输入码云账号密码的对话框。部署成功之后进入自己的码云账号,查看之前创建的项目中出现了本地项目中public文件夹中的文件

    然后开启码云的Pages功能 服务→gitpage→启动 访问链接试试看 如果主题网页体现不出来,留意启动配置页面那个使用帮助 设置好URL url: 你仓库地址 root: 你项目地址 部署到远端 hexo d 完成 https://仓库名.gitee.io/项目名/ 主题更换 下载zip包解压在themes文件夹下或git clone下来 在博客目录中更改 theme: 主题名(默认是landscape) 清理并生成 hexo clean hexo g 调试并部署 hexo d

    来源:https://www.cnblogs.com/zxkwdw/p/11370311.html

    read more
  • Quite new to metaprogramming (if the word can be used here); I\'ve stripped down to the bare minimum the following example :

    module A PROCESSES_KEYS = [] def process(name) PROCESSES_KEYS.push(name) send :define_method, :processes do pp PROCESSES_KEYS end send :define_method, name do pp \"method #{name} is available\" end end end class B extend Helper process :a process :b process :c end

    From those methods, I\'d be looking to be able to list those at the same time.

    X.new.props # => [:a, :b, :c]

    The example do work, but I\'m pretty sure it is not the best way to go.

    How such thing could be correctly achieved ?

    read more
  • I'm just trying to make a simple connection to another page using the url tag in Django. I'm getting a error of "'set' object is not reversible". After searching for a bit I've been unsuccessful in finding anything.

    urls.py

    from django.conf.urls import url from . import views APP_NAME = 'website' urlpatterns = { url(r'^$', views.admin_view, name='adminview'), url(r'^eventview/$', views.event_view, name='eventview'), }

    admin_view.html

    <html lang="en" > <head> {% load static %} {% block header%} {% include 'website/header.html' %} {% endblock %} <!-- Insert custom css here --> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <!-- top navbar --> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Vivid Fireworks</a> </div> <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav navbar-right"> <li><a href="{% url adminview %}">Dashboard</a></li> <li><a href="{% url eventview %}">Add Show</a></li> <li><a href="#">Settings</a></li> <li><a href="#">Profile</a></li> <li><a href="#">Help</a></li> </ul> </div> </div> </nav>

    I haven't ran into this problem before and it seems like it'll be a simple fix just something I'm over looking. Any help is appreciated.

    urlpatterns should be a list [...]. You currently have a set {...}. It should be:

    urlpatterns = [ url(r'^$', views.admin_view, name='adminview'), url(r'^eventview/$', views.event_view, name='eventview'), ]

    In the template, you should use quotes when the url pattern name is a string:

    {% url 'adminview' %} {% url 'eventview' %}

    If you want to use namespaces, then app_name should be lowercase.

    app_name = 'website' url_patterns = [ ... ]

    You then need to include the namespace when you use the url tag

    {% url 'website:adminview' %} {% url 'website:eventview' %}

    来源:https://stackoverflow.com/questions/43184081/what-does-it-mean-by-object-not-reversible-django

    read more

最新主题

316
Online

338
Users

226.1k
Topics

379.4k
Posts

在线用户

推荐阅读

本站部分内容来自互联网,其发布内容言论不代表本站观点,如果其链接、内容的侵犯您的权益,烦请联系我们,我们将及时予以处理。
Powered by NodeBB | 备案号:宁ICP备15000671号