Home > other >  Using python selenium to choose an option from a dropdown menu with always changing values
Using python selenium to choose an option from a dropdown menu with always changing values

Time:12-30

I am struggling with a code.

I want to choose an option from a dropdown menu, but the options' values are always different.

I was using this pseudo code I found here:

driver.find_element_by_xpath("//option[text()="Some text"]").click()

This works for now, but the "Some text" is always changing, depending on the date.

This is the HTML of the dropdown menu (see picture attached). As described, the values change regarding the date, but the date always changes when I try to automate this to run once a day (obviously). dropdown HTML

How can I solve this? Lets say I'd like to always choose the 6:30 pm option?

Thanks in advance.

CodePudding user response:

You can get the element like this (python example):

from datetime import datetime, timedelta

object_time = datetime.today().replace(hour=6, minute=30, second=0)
object_name = object_time.strftime("%Y-%m-%d %H:%M:%S")

driver.find_element_by_xpath(f"//option[value='{object_name}']").click()

CodePudding user response:

Heres the part which doesnt work.

My code where it doesnt work

  • Related