0
public class WebDriverDemo {
    public static void main(String\[\] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kartikeye Pandey\\Desktop\\Selenium\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");
        driver.findElement(By.cssSelector("a\[aria-label='Google apps'\]")).click();
        //driver.findElement(By.cssSelector("\[class='gb_d'\]\[data-pid='23'\]")).click();
        //driver.findElement(By.cssSelector("\[class='tX9u1b'\]\[data-pid='36'\]")).click();
        driver.findElement(By.cssSelector("span\[class='MrEfLc'\]")).click();
    }
}

I want that on google webpage clicking on the three dots is working but clicking into it's submenu is not working. Suppose from three dots I want to open Youtube but it is not working

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

2 Answers2

1

The element/icon with the link to YouTube 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:
    • Using cssSelector:

      driver.get('https://www.google.com/');
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[aria-label='Google apps']"))).click();
      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src*='https://ogs.google.com/u/0/widget/app']")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//li/a//span[text()='YouTube']"))).click();
      
    • Using xpath:

      driver.get('https://www.google.com/');
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@aria-label='Google apps']"))).click();
      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@src, 'https://ogs.google.com/widget/app')]")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//li/a//span[text()='YouTube']"))).click();
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • If you are using Selenium 4, `WebDriverWait` doesn't take an integer directly for the time to wait, you need to use `Duration` class. For the actual time to wait `Duration.ofSeconds()` and for the polling interval, `Duration.ofMillis()` – hfontanez Mar 24 '22 at 12:24
  • 1
    @hfontanez True, agree with you. – undetected Selenium Mar 24 '22 at 12:26
0

The reason is when you want to locate the youtube button for clicking, there are another buttons with the same locator. So Selenium can not click the exact element.

You can try with below xpath and renew your locators.

Google Apps

(//a[contains(@aria-label, 'Google apps')])

Youtube

(//a[@href="https://www.youtube.com/"])

you need to pause a while for locate youtube button because it dissappers when mouse move anıther direction. So I opened the debugger on the console side just type'in the below command

setTimeout(()=>{debugger;},5000);