Search code examples
pythonseleniumselenium-webdriverselenium-chromedriverpython-webbrowser

Trouble selecting google one box tab in selenium


going to https://www.google.com/search?q=tennis a google one box comes up for scores, I'm trying to click on the tabs and (Women's Singles, Men's Doubles etc) and having no luck. I've tried all methods, xpath, classname, partial link text, css selector.

enter image description here

any help would be appreciated!!!


Solution

  • The desired element is a JavaScript enabled element, so ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

    • Clicking Women's Singles:

      driver.get('https://www.google.com/search?q=tennis')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@jsslot]//span//span[contains(., 'Women') and contains(., 'Singles')]"))).click()
      
    • Clicking Men's Doubles:

      driver.get('https://www.google.com/search?q=tennis')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@jsslot]//span//span[contains(., 'Men') and contains(., 'Doubles')]"))).click()
      
    • Note: You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC