What is the best way to avoid NoSuchElementException in Selenium?

后端 未结 9 623
时光说笑
时光说笑 2020-11-27 18:31

I have written few test cases in Selenium WebDriver using Java and execute them on grid (hub and multiple nodes). I have noticed that a few test cases fail due to NoSu

相关标签:
9条回答
  • 2020-11-27 19:04

    Sometimes it is possible to wait for the download of the desired item.

    driver.get("https://zzzzzzzzz.market/items/mirage_prime_set")
    
    WebDriverWait(driver, 20)
           .until(
            EC.visibility_of_element_located(
              (By.XPATH, ('//div[@class="orders-row__element order__price sell_color"]')
            )))
    

    Sometimes you need to do something so that the UI framework loads the data. For example scroll page

    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    

    And then get the necessary data

    responsetext=driver.page_source
    
    from lxml import html
    parsed_body = html.fromstring(responsetext)
    
    obj1 = parsed_body.xpath('.//div[@class="orders-row__element order__price sell_color"]/span[1]')
    print(len(obj1))
    
    0 讨论(0)
  • 2020-11-27 19:07
    WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
    wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));
    

    elementToBeClickable waits for Enable and Visible of an Element

    0 讨论(0)
  • 2020-11-27 19:08

    you can also use FluentWait,

    Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.

    Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

    // Waiting 30 seconds for an element to be present on the page, checking
       // for its presence once every 5 seconds.
       Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
           .withTimeout(30, SECONDS)
           .pollingEvery(5, SECONDS)
           .ignoring(NoSuchElementException.class);
    
       WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
         public WebElement apply(WebDriver driver) {
           return driver.findElement(By.id("foo"));
         }
       });
    

    Click here for more info

    0 讨论(0)
  • 2020-11-27 19:08

    We can apply below codes to remove this exception condition

    1. By applying WebDriverWait, webdriver object wait for a specific time (in second) of an element for its visibility.

            WebDriverWait wait = new WebDriverWait(driver, 10);       
             wait.until(ExpectedConditions.visibilityOf(link));
      
    2. We can handle NoSuchElementException through try-catch block inside Generic method

       public boolean isElementPresent(By by) {
       boolean isPresent = true;
       try {
       driver.findElement(by);
       } catch (NoSuchElementException e) {
        isPresent = false;
       }
      return isPresent
      }
      

    http://selenium-code.blogspot.in/2017/08/selenium-exception-nosuchelementexcepti.html

    0 讨论(0)
  • 2020-11-27 19:10

    NoSuchElementException occurs, when the locators (i.e. id / xpath/ css selectors) is unable to find the web element on the web page.

    The reasons for this could be :

    1. Incorrect Locator

    2. Web element not available on web page

      In order to avoid this exception, we can use Fluent Wait. This wait allows us to define max timeout, polling frequency and define which exception to ignore.

    Please find the sample usage of Fluent wait below :

    .withTimeout(50, TimeUnit.SECONDS)
    .pollingevery(3, TimeUnit.SECONDS)
    .ignoring(NoSuchElementException.class);
    
    0 讨论(0)
  • 2020-11-27 19:13

    You can never be sure that element will be found, actually this is purpose of functional tests - to tell you if anything changed on your page. But one thing which definitely helps is to add waits for the elements which are often causing NoSuchElementException like

    WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
    
    0 讨论(0)
提交回复
热议问题