Home > Enterprise >  How to wait for img src attribute to change using Selenium and JavaScript?
How to wait for img src attribute to change using Selenium and JavaScript?

Time:11-23

let's say I have an element:

<div>
 <img src='src1'>
</div>

and after some time src changes to some other link
so how to I wait for it (or any property in general) to change?

maybe something like this:

await driver.wait(until.elementAttributeChanged(el,'src'),3000);

CodePudding user response:

To wait for the src attribute of the <img> to change using Selenium and you can use the following until condition:

  • stalenessOf( element ): Creates a condition that will wait for the given element to become stale. An element is considered stale once it is removed from the DOM, or a new page has loaded.

    let element = await driver.findElement(By.id('elementID'));
    await driver.wait(until.stalenessOf(element),1000);
    

CodePudding user response:

Just to make DebanjanB's answer more precise.
Since we want to indicate the changing of src attribute value of some element, we can use locator based on the value of src attribute of that element. So the code can be something like this:

let element = await driver.findElement(By.xpath('//img[@src="src1"]'));
await driver.wait(until.stalenessOf(element),1000);
  • Related