Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selenium fields containing a word
#1
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
Reply
#2
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.
Recommended Tutorials:
Reply
#3
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
Reply
#4
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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Registration Form Fields rwahdan 2 1,835 Aug-09-2022, 10:35 AM
Last Post: Addweb
  [Intermediate] Key Word Scrapper with Python and Selenium MIPython 0 5,726 Dec-21-2017, 03:35 PM
Last Post: MIPython
  Error in Selenium: CRITICAL:root:Selenium module is not installed...Exiting program. AcszE 1 3,584 Nov-03-2017, 08:41 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020