Asked  7 Months ago    Answers:  5   Viewed   73 times

I'm working on an Extension in Chrome, and I'm wondering: what's the best way to find out when an element comes into existence? Using plain javascript, with an interval that checks until an element exists, or does jQuery have some easy way to do this?



DOMNodeInserted is being deprecated, along with the other DOM mutation events, because of performance issues - the recommended approach is to use a MutationObserver to watch the DOM. It's only supported in newer browsers though, so you should fall back onto DOMNodeInserted when MutationObserver isn't available.

let observer = new MutationObserver((mutations) => {
  mutations.forEach((mutation) => {
    if (!mutation.addedNodes) return

    for (let i = 0; i < mutation.addedNodes.length; i++) {
      // do things to your newly added nodes here
      let node = mutation.addedNodes[i]

observer.observe(document.body, {
    childList: true
  , subtree: true
  , attributes: false
  , characterData: false

// stop watching using:
Tuesday, June 1, 2021
answered 7 Months ago

You need to call ignoring with exception to ignore while the WebDriver will wait.

FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)
        .withTimeout(30, TimeUnit.SECONDS)
        .pollingEvery(200, TimeUnit.MILLISECONDS)

See the documentation of FluentWait for more info. But beware that this condition is already implemented in ExpectedConditions so you should use

WebElement element = (new WebDriverWait(driver, 10))

*Update for newer versions of Selenium:

withTimeout(long, TimeUnit) has become withTimeout(Duration)
pollingEvery(long, TimeUnit) has become pollingEvery(Duration)

So the code will look as such:

FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)

Basic tutorial for waiting can be found here.

Thursday, June 3, 2021
answered 6 Months ago

You can also use -

new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(locator));

If you go through the source of it you can see that both NoSuchElementException and staleElementReferenceException are handled.

   * An expectation for checking that an element is either invisible or not
   * present on the DOM.
   * @param locator used to find the element
  public static ExpectedCondition<Boolean> invisibilityOfElementLocated(
      final By locator) {
    return new ExpectedCondition<Boolean>() {
      public Boolean apply(WebDriver driver) {
        try {
          return !(findElement(locator, driver).isDisplayed());
        } catch (NoSuchElementException e) {
          // Returns true because the element is not present in DOM. The
          // try block checks if the element is present but is invisible.
          return true;
        } catch (StaleElementReferenceException e) {
          // Returns true because stale element reference implies that element
          // is no longer visible.
          return true;
Thursday, July 1, 2021
answered 5 Months ago

You have one fundamental misunderstanding: your background script can't modify the webpage, because document refers to the background page itself.

Take a look at the Architecture overview. After you do, you should know that you need a Content Script.

Once we've established that, your content script can independently track where the click happened - and that would contain a reference to the element. Then, when your background requests a modification, you know the last such element.

There is a very good question discussing your problem in great detail too.

Sunday, October 17, 2021
answered 2 Months ago
function queryParams(source) {
  var array = [];

  for(var key in source) {
     array.push(encodeURIComponent(key) + "=" + encodeURIComponent(source[key]));

  return array.join("&");
Monday, November 1, 2021
Nona Urbiz
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :