-1

I am trying to locate image using Selenium webdriver but unable to locate it by Xpath/cssSelector

I have tried cssSelector and xpath but not working.

<img alt="" class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://tpc.googlesyndication.com/simgad/303052068860032968">

By cssSelector -->

WebElement elementOut = driver.findElement(By.cssSelector(".i-amphtml-fill-content.i-amphtml-replaced-content"));

By Xpath -->

WebElement elementOut = driver.findElement(By.xpath("//*[@id='aw0']/amp-img/img"));

I need to locate the image.

Snapshot of the page source:

Detailed page source

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Can you please post the HTML code of the page where your image is located ? Otherwise, it will be impossible to help you on this. – mr.mams May 24 '19 at 15:24
  • HTML Code : - – Abhijit Joshi May 24 '19 at 15:49
  • This is only the `` tag. This is not enough to locate the image. What you need to provide is the HTML code of the entire page, or at least all the HTML tags enclosing the image. Also, please add that code inside your original post (you should be able to edit it, rather than in a comment). This will make it more readable, and people will be able to help you more easily. – mr.mams May 24 '19 at 15:59
  • Added link for detailed page source – Abhijit Joshi May 24 '19 at 16:53
  • You need to select the `iframe` first! – SiKing May 24 '19 at 23:54

2 Answers2

1

Your image resides within an iframe

enter image description here

So you will need to execute driver.switchTo() function prior to attempting to locate element which is inside the iframe.

Once done you should be able to use the XPath expression like:

driver.findElement(By.xpath("//img[contains(@class,'replaced-content')]"));
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • As suggested i have switched to iframe and tried to locate the element but still i am unable to locate the element. My code as below: driver.switchTo().frame("google_ads_iframe_/8900/24.com/Web/News24/Homepage_20");driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);WebElement elementOut = driver.findElement(By.xpath("//div[contains(@id,'DfaVisibilityIdentifier_3594674463')]")); – Abhijit Joshi May 25 '19 at 10:59
  • They have updated code so my xpath changed from previous version of code. – Abhijit Joshi May 25 '19 at 11:03
0

To click() on the image, as the the desired element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
  • Induce WebDriverWait for the desired elementToBeClickable.
  • You can use either of the following Locator Strategies:

    • cssSelector:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id$='com/Web/News24/Homepage_20'][name^='google_ads_iframe_'][title='3rd party ad content']")));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("img.i-amphtml-fill-content.i-amphtml-replaced-content[src^='https://tpc.googlesyndication.com/simgad']"))).click()
      
    • xpath:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@id, 'com/Web/News24/Homepage_20') and starts-with(@name, 'google_ads_iframe_')][@title='3rd party ad content']")));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//img[@class='i-amphtml-fill-content i-amphtml-replaced-content' and starts-with(@src, 'https://tpc.googlesyndication.com/simgad')]"))).click();
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352