Ravneet Kaur
Ravneet Kaur

Reputation: 21

Not able to find an element in iframe using Xpath and Selenium

I am trying to inspect an element in Iframe I am able to succesfully get into iframe but when trying to inspect element -Exception comes element not intertracble Below is the html part and the search button I am trying to click enter image description here

I tried xpath and inspection like following

//driver.findElement(By.className("gsc-search-button-v2")).click();
        //driver.findElement(By.cssSelector("button.gsc-search-button")).click();
        //driver.findElement(By.xpath("//button[text()='gsc-search-button']")).click();

        //driver.findElement(By.cssSelector("button[class='gsc-search-button gsc-search-button-v2']")).click();
//driver.findElement(By.cssSelector(".gsc-search-button.gsc-search-button-v2")).click();

//driver.findElement(By.xpath("//button[contains(@class, 'gsc-search-button gsc-search-button-v2")).click();
//wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".gsc-search-button.gsc-search-button-v2"))).sendKeys(Keys.ENTER);

Also by giving waits also like
WebDriverWait wait = new WebDriverWait(driver, 30);
    //wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".gsc-search-button.gsc-search-button-v2"))).sendKeys(Keys.ENTER);

html code enter image description here

Upvotes: 0

Views: 1308

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193108

To click() on the element to search, as the the desired element is within a <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:
    • Using cssSelector:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe_cssSelector")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td.gsc-search-button > button.gsc-search-button.gsc-search-button-v2"))).click();
      
    • Using xpath:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe_xpath")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@class='gsc-search-button']/button[@class='gsc-search-button gsc-search-button-v2']"))).click();
      

Reference

You can find a couple of relevant discussions in:

Upvotes: 1

Related Questions