Selenium fields containing a word - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Selenium fields containing a word (/thread-20008.html) |
Selenium fields containing a word - graham23s - Jul-23-2019 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 RE: Selenium fields containing a word - metulburr - Jul-23-2019 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. RE: Selenium fields containing a word - graham23s - Jul-23-2019 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 RE: Selenium fields containing a word - Gaurav_Kumar - Aug-22-2023 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") |