Home > Net >  Why is Selenium not accepting dynamic variables in this function?
Why is Selenium not accepting dynamic variables in this function?

Time:12-16

Why does this work:

monday_div = driver.find_element(By.XPATH, '//*[@id="GXPMonday"]')

but this does not?

weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

for day in weekdays:
    main_day_div = driver.find_elements(By.XPATH, '//*[@id="GXP'   day   '"]')

I get the following error

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="GXPMonday"]

This is the website I'm trying to parse: https://movatiathletic.com/clubs/schedule.php?acct=663&loc=2860

CodePudding user response:

Try this...

main_day_div = driver.find_element(By.ID,f"GXP{day}")

If this doesn't work, you may be running into a timing issue where you would be better served by using expected_conditions.

from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

wait = WebDriverWait(driver, timeout=30)
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

for day in weekdays:
    main_day_div = wait.until(ec.presence_of_element_located((By.ID,f"GXP{day}")))

CodePudding user response:

Your locator is correct, you need to add some wait time, just added the code to print the GXPTitle for your reference, check it:

weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    
for day in weekdays:
    main_day_div = WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="GXP'   day   '"]//h6')))
    print("Total titles in -", day ,":", len(main_day_div))
    print("-------")
    for title in main_day_div:
        print(title.text)
    print("=======")
  • Related