0

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ravneet Kaur
  • 21
  • 1
  • 6
  • Have you tried copying full xpath of the button? – testing_22 Jan 20 '21 at 13:12
  • yes full xpath is the relative xpath that works fine but what if need to create by ourself?for something like – Ravneet Kaur Jan 20 '21 at 13:18
  • I think you need to visualize how DOM's tags are organized in order to create the xpath by yourself. Just place the tags separated by slash until you get to the desired element. Notice that some are indexed as td[2] above, which means the button is in the second td tag. – testing_22 Jan 20 '21 at 13:29

1 Answers1

1

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352