1

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!!!

2 Answers2

1

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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0

I always use this method to set the XPath in Selenium based Python projects.

  1. Click F12 after opening the link
  2. In Elements, Click Ctrl + F. Now search some unique term in the page. I gave Men's Singles and this snippet came up.
<div class="SVWlSe FZzi2e" jsslot="">
    <span jsslot="">
       <div aria-controls="_dtAkYIqaMLraz7sPnrW54A435_0" 
            id="_dtAkYIqaMLraz7sPnrW54A434_0">
            <span>
                <span>Men's Singles</span>
             </span>
       </div>
    </span>
</div>
  1. Now right-click the snippet to get the XPath format or use the XPath Syntax. Here, the XPath is //div[@class='SVWlSe FZzi2e']. To find whether the attribute value is unique, I would suggest you to search within elements again with the value.

Notice that I used the class attribute instead of the aria-controls and id attributes since id and aria-controls changed over time.

Sarath
  • 73
  • 9