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"))
    )
    
    


最新内容

  • Answering My Own Question Here.

    Here is my working code / what I ended up doing (welcome criticism/improvement ideas), including my own comments added:

    <script> // Gets the URL of a link into the anchor text and removes extra characters. // Notes: // - This could most likely be done more elegantly since they are referencing the same element! // - This was done in a WP/Elementor page with weird selectors. var myanchor = document.querySelector(".partner-website-url.elementor-widget.elementor-widget-heading a"); var myurl = document.querySelector(".partner-website-url.elementor-widget.elementor-widget-heading a").href; var cleanurl = myurl.replace(/http(s)://(www\.)/, '').replace(//+$/, ''); myanchor.innerHTML += cleanurl; </script>

    read more
  • I'm not sure if I understand but you should use .innerText, also if you want to use that query you should use cleanurl[0] the error is because cleanurl it is not a String so .replace() does not work.

    <pre class="snippet-code-js lang-js prettyprint-override">``` var a = document.querySelector("#partner-website-element a"); var url = a.innerText; a.innerText = url.replace(/https?:\/\/w{3}\./gi,"");

    <pre class="snippet-code-html lang-html prettyprint-override">```
    <div class="elementor-element elementor-element-0e4572c elementor-widget elementor-widget-heading" data-id="0e4572c" data-element_type="widget" id="partner-website-element" data-widget_type="heading.default">
    <div class="elementor-widget-container">
    <h5 class="elementor-heading-title elementor-size-default">
    <a href="https://www.theurl.com">Visit https://www.theurl.com</a>
    </h5>
    </div>
    </div>

    read more
  • Wait until the page loads before accessing the DOM Access the first element of the NodeList returned by getElementsByTagName Clean the url and set it back document.onLoad = function () { var ele = document.getElementById("partner-website-element").getElementsByTagName('a'); ele[0].innerHTML = ele[0].innerHTML.replace(/http(s):\/\/(www)/gi, ''); }

    read more

最新主题

推荐阅读

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