enter image description hereI have tried many different ways to try and switch to the iframe and did figure out that it's the 2nd iframe, but no matter what I do when I run my automation it doesn't acknowledge that it's there. I have tried css selectors, and different xpath selectors. I know how to write my own xpath and have tried adding the parent div as well but that didn't work. I added an image of the code as I cannot copy and paste it easily.

My different approaches:

  1. WebElement frameElement = driver.findElement(By.xpath("//iframe[@src='https://www.youtube.com/embed/pM8Xg49zEqA']")) driver.switchTo.frame(frameElement)

Error: [EXCEPTION] NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[@src='https://www.youtube.com/embed/pM8Xg49zEqA']"}

  1. WebElement iframe = driver.findElement(By.tagName("iframe")) driver.switchTo().frame(iframe) driver.findElement(By.cssSelector(".ytp-large-play-button")).click()

Error: [EXCEPTION] NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".ytp-large-play-button"} (Session info: chrome=80.0.3987.163)

  1. WebDriverWait wait = new WebDriverWait(driver, 120); List adFrame = driver.findElements(By.xpath("//iframe[@src='https://www.youtube.com/embed/pM8Xg49zEqA']")) if(adFrame.size()>0) { driver.switchTo().frame(adFrame.get(0)) wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//button[@class='ytp-large-play-button ytp-button']")))).click() } else { System.out.println("Sorry there is no video") }

Error: Sorry there is no video

  1. WebUtils.scrollByPixel(0, 400, browser) WebElement frame= driver.findElement(By.xpath("//iframe[@src='https://www.youtube.com/embed/pM8Xg49zEqA']")); driver.switchTo().frame(2)

Error: [EXCEPTION] NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[@src='https://www.youtube.com/embed/pM8Xg49zEqA']"}

  1. ![when: WebUtils.scrollByPixel(0, 400, browser) waitFor() {celaviveVideoFrame.isDisplayed()} celaviveVideoFrame.isFocused() celavivePwpVideo.click()]2

Error: [EXCEPTION] NullPointerException: Cannot invoke method waitFor() on null object Web.ui.PWP.CelaviveVideoSpec.Step 6: Can click on Celavive Video(CelaviveVideoSpec.groovy:88)

  • are you getting any error – PDHide Apr 9 at 6:40
  • Yes sorry I forgot to post that. I will comment the error after each method that I tried. – ameuzela Apr 9 at 17:39
  • I added the errors. – ameuzela Apr 9 at 17:46
  • why u have the @ symbol in ur locator remove that and I try – PDHide Apr 9 at 17:48
  • Tried that as well and it didn't work. I do have a new better method that I will post in the comments although still getting an error. I’m wondering if Selenium can even click on an iframe button from another domain. So from our domain to YouTube’s domain. – ameuzela Apr 11 at 3:58

Can you try adding driver.switchTo().defaultContent() before trying to switch to the iframe?

E.g., for your first approach:

    WebElement frameElement = driver.findElement(By.xpath("//iframe[@src='https://www.youtube.com/embed/pM8Xg49zEqA']"));
| improve this answer | |
  • Hi I tried that but it didn't work. – ameuzela Apr 11 at 3:56

Here is my new code that actually clicks on the button but getting the following error: Condition not satisfied - null

 WebUtils.scrollByPixel(0, 400, browser)
    def element = waitFor {driver.findElement(By.xpath(".//iframe[@src='https://www.youtube.com/embed/pM8Xg49zEqA']"))}
    WebDriverWait wait = new WebDriverWait(driver,30);
    JavascriptExecutor executor = (JavascriptExecutor) driver
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.