2

Whenever I'm using this code:

driver.findElement(By.xpath("//*[@id='gbwa']/div[1]/a")).click();

its navigating to "products" page. but I want the dropdown list in the homepage itself to select youtube. I have also tried that process:

List<WebElement> li=driver.findElements(By.cssSelector(".gb_ia"));
WebElement r=driver.findElement(By.cssSelector("#gb36 > span.gb_3"));
for(WebElement option:li){
    if(option.equals(r)){
        option.click();
        break;
    }
}

Its also not working. Help plz...

2 Answers 2

1

That should do the trick:

driver.get("http://www.google.com");

WebElement apps = new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.id("gbwa")));
apps.click();

WebElement youtube = new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[.='YouTube']")));
youtube.click();

Note that the wait statements can be crucial, as otherwise the tests may fail depending on page load times, network connection, or other reasons that are not within your control.

4
  • No, its not working...its again navigating to "google products" before opening the list where we can find 'youtube' button...so after executing apps.click(); , its showing org.openqa.selenium.NoSuchElementException
    – MrPhoenixB
    Commented Feb 26, 2017 at 12:00
  • Which browser/operating system/WebDriver version are you running this on? For me (Chrome 56, Win10, Selenium 3.0.1) that works perfectly fine.
    – ralph.mayr
    Commented Feb 26, 2017 at 13:32
  • i was using(Firefox 45.03 dev edition, Win10, Selenium 3.0.1)....now just checked in chrome 56....its working fine! Thanks for your kind help....btw, do you have any idea why it was not working in firefox?
    – MrPhoenixB
    Commented Feb 26, 2017 at 14:09
  • You're right - in Firefox, the first click doesn't actually open the "Apps" menu, hence the second find times out. This seems to be because the "Apps" button only handles the click if the mouse is currently over it. Chrome seems to be simulating that better than Firefox. (You can validate that by stepping through the test in the debugger and moving the mouse over the Apps button before executing the line with the click, and you'll see that the click actually works then.)
    – ralph.mayr
    Commented Feb 26, 2017 at 16:35
1

This should work even in firefox:

Actions action = new Actions(driver);
action.click(driver.findElementByXPath("//*[@id='gbwa']/div[1]/a")).build().perform();

Below will click the news links in apps menu.

driver.findElementByXPath("//div[@id='gbwa']/div[2]/ul[1]/li[6]/a").click();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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