Search code examples
python-3.xseleniumxpathwebdriverwaitexpected-condition

How to click on the Ask to join button within https://meet.google.com using Selenium and Python?


I am trying to click the Ask to join button in a google meet link(using my existing Google Chrome profile).This is the code:

options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\\Users\\Pranil.DESKTOP-TLQKP4G.000\\AppData\\Local\\Google\\Chrome\\User Data")
browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)
delay = 15
browser.get('https://meet.google.com/tws-kcie-aox')
ignored_exceptions=(NoSuchElementException,StaleElementReferenceException,)
time.sleep(5)
join_butt = WebDriverWait(browser, delay ,ignored_exceptions=ignored_exceptions).until(EC.presence_of_element_located((By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/div[5]/div[3]/div/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div')))
join_butt.click()
print(join_butt.text)#this prints Ask to join

But the join button is not getting clicked. The most weird part 'Ask to join' text on the button does get printed in the last line. This means that selenium has reached the correct button. But still why does it not click the button?

EDIT: According to an answer by @Alin Stelian I updated the code as follows:

browser.get('https://meet.google.com/hst-cfck-kee')
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'd')
join_butt = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, "//span[contains(text(),'Ask to join')]")))
join_butt.click()
print(join_butt)
print(join_butt.text)

This does work as both the print statements work...But the button isn't getting clicked. What is going wrong over here?


Solution

  • For further automating projects - avoid finding elements by id when the value is generated programmatically - it will not help you. Also, long xpaths is bad for your project performance.

    The performance level of locators is -> ID, CSS, XPATH.

    join_butt = WebDriverWait(browser, delay ,ignored_exceptions=ignored_exceptions).until(EC.presence_of_element_located((By.XPATH, '//span[contains(text(),'Ask to join')]')))

    later edit next time don't ignore exceptions - it will help you to see your error syntax, I tested myself the below code.

    join_butt = WebDriverWait(browser, delay).until(
        EC.presence_of_element_located((By.XPATH, "//span[contains(text(),'Ask to join')]")))
    driver.execute_script("arguments[0].click();", join_butt)
    

    If the chrome browser doesn't allow you to log in - here is a trick

    1. Run your code
    2. In that browser go to StackOverflow
    3. Login w/ your account
    4. Quit the browser
    5. Run again your code - now you'll be logged in automatically in your google account.