Python Forum

Full Version: Selenium fields containing a word
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Guys,

I was wondering if it is possible using Selenium to interact with a field if it contains a word, for example:

<select id="field_10_month" name="field_10_month"
<select id="field_10_day" name="field_10_day"
<select id="field_10_year" name="field_10_year"

With the fields above if the first one contained the word: month then i would know in the script to select a month, the same idea would go for day and year

Is this even possible with selenium at the minute?

cheers guys

Graham
you mean something like this?
value = 'month'
browser.find_element_by_id('field_10_{}'.format(value))
Otherwise could you elaborate a little more? I not really sure on what your asking.
Hello :)

Kind of like:

if self.driver.find_element_by_xpath("//select[contains(text(), 'month')]"):
    print("FOUND! an element that contains the word 'month' has been found!")
    select = Select(self.driver.find_element_by_xpath("//select[contains(text(), 'month')]"))
select.select_by_index(2)
In this mockup if the find element contains a keyword (month in this example) it will randomly select a drop down value.

Does this make sense ok? :)

regards

Graham
Yes, you can achieve this using Selenium in combination with Python. You can use the find_element_by_id method to locate the <select> elements by their id attribute, and then you can use the get_attribute method to retrieve the name attribute value.

Like this:-

month_select = driver.find_element_by_id("field_10_month")
day_select = driver.find_element_by_id("field_10_day")
year_select = driver.find_element_by_id("field_10_year")
month_name = month_select.get_attribute("name")
day_name = day_select.get_attribute("name")
year_name = year_select.get_attribute("name")