Utilizing Selenium and Python to Choose an Option from a Dropdown Menu

How can I extract the visible text 'planned works' from this HTML drop-down list?

<select id="NewEnquiry.Purpose" data-bind="options: SelectablePurposes, value: Purpose, optionsText: 'Name', optionsCaption: '-- Please Select --', validationOptions: { rule: 'Enquiry.Purpose' }"><option value="">-- Please Select --</option><option value="">Planned Works</option><option value="">Initial Enquiry</option><option value="">Emergency</option></select>
<option value>-- Please Select --</option>
<option value>Planned Works</option>
<option value>Initial Enquiry</option>
<option value>Emergency</option>

The code I've tried so far doesn't seem to be working:

select = Select(driver.find_element_by_id('//*[@id="NewEnquiry.Purpose"]'))
select.select_by_visible_text('Planned Works')

I am also using this import statement:

from selenium.webdriver.support.ui import Select

Any advice or suggestions on how to solve this issue?

Answer №1

When dealing with a <select> element that is dynamic, selecting the <option> with the text Planned Works requires using WebDriverWait and the element_to_be_clickable() method. You can utilize either of these locator strategies:

  • For CSS_SELECTOR:

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[id^='NewEnquiry'][data-bind*='SelectablePurposes']")))).select_by_visible_text('Planned Works')
    
  • For XPATH:

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[starts-with(@id, 'NewEnquiry') and contains(@data-bind,'SelectablePurposes')]")))).select_by_visible_text('Planned Works')
    
  • Important: Make sure to import the following:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Additional Resources

You may find useful discussions in:

  • How to select an option from the dropdown menu using Selenium and Python

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting JSON data to a pandas DataFrame requires the list indices to be integers

I have a large JSON dataset that I want to convert to CSV for analysis purposes. However, when using json_normalize to build the table, I encounter the following error: Traceback (most recent call last): File "/Users/Home/Downloads/JSONtoCSV/easybill.py" ...

Issue encountered while creating code - Python Selenium Type Error

My code is throwing an error message that says "selenium.common.exceptions.WebDriverException: Message: TypeError: BrowsingContextFn().currentWindowGlobal is null." Can someone assist me with this? Here is the snippet of my code: from instapy import Insta ...

Using Python Selenium to Upload Images with Chromedriver

My attempts to upload images through Python Selenium have hit a snag. Initially, the process worked flawlessly, but now the button ID is randomly generated each time. I've experimented with various CSS paths and XPaths in an effort to get it working a ...

Transferring information between Flask and JS using AJAX for a Chrome extension

I'm experimenting with AJAX calls to establish communication between my Javascript frontend in a chrome extension and the Flask API where I intend to utilize my Machine Learning algorithms. content.js console.log("Let's get this application ...

Error: The specified element is not found in the list when attempting to remove it, although it actually exists within the

I am currently going through the O'Reilly book "Exploring Everyday Things in R and Ruby" and my task is to convert all of the Ruby code into Python. I started with a model that determines the number of bathrooms required for a building, and here' ...

What is the process for obtaining the most recent stock price through Fidelity's screening tool?

Trying to extract the latest stock price using Fidelity's screener. For instance, the current market value of AAPL stands at $165.02, accessible via this link. Upon inspecting the webpage, the price is displayed within this tag: <div _ngcontent-cx ...

Incorporating fresh CSS styles through Selenium

I am attempting to showcase all the prices available on this page using Selenium and Chrome in order to capture a screenshot. By default, only 3 prices are visible. Is there a way to disable the slick-slider so that all 5 prices can be seen? I have tried r ...

Converting a DataFrame into a dictionary in Python

I am attempting to recreate a bokeh bar chart with nested categories that is shown on this page: Starting with the dataframe below test__df = pd.DataFrame(data= [['2019-01-01','A',1], ['2019-01-01 ...

What is the best way to interact with a random pop-up while running a loop?

While running my Python scraping script, I encounter a scenario where the script fails due to random pop-ups appearing on the page while collapsing multiple buttons. Although I have already handled these two pop-ups at the beginning of the script, the web ...

Incomplete roster of kids using Selenium in Python

Does anyone have a solution for retrieving all the children of an element in HTML? Here is the structure of the html: Check out this image of the html structure I've tried the following code: time.sleep(30) stocks = browser.find_element_by_class_na ...

Creating a folder for a particular purpose in C#: A step-by-step guide

I have integrated Selenium for automation purposes. I have structured my code with a screenshot class and Page Object method to organize elements in separate classes for each page. Currently, I am taking screenshots of each page by calling the get screensh ...

Failed to execute on [cor-001]: unable to proceed because junos-eznc (PyEZ) version 2.1.7 or higher is necessary for this module. Unfortunately, junos-eznc is not available

I encountered an error while trying to execute my playbook, despite having pyEZ installed. Can someone assist me in resolving this issue? MCBOOK:~ user2018$ ansible-playbook simpletest.yml [DEPRECATION WARNING]: [defaults]hostfile option, The key is ...

What is the process for executing Selenium IDE scripts in Selenium RC?

As a newcomer to using the selenium testing tool, I am seeking guidance on running selenium IDE scripts within selenium RC. I would greatly appreciate examples and screenshots to help me better understand the process. ...

Guide on extracting information from a JSON array consisting of objects and adding it to a Pandas Dataframe

Working with Jupyter Notebook and handling a Batch API call that returns a JSON array of objects can be tricky. The parsing process involves using for loops, which may seem weird at first. In my case, I needed to extract specific JSON object information an ...

Understanding JSON: Pairing Keys with Values

Is it possible to match the keys with the corresponding values in a JSON file? Check out this JSON file here View an image of the data structure I am referencing The keys are located in the "fields" section, while the values can be found in the "data" s ...

What could be causing the Selenium web driver to crash while trying to navigate?

Situation : For our ASP.NET MVC application, we decided to run integration tests using Selenium's Web Driver. Issue : Upon running the tests in VS2015 with Internet Explorer Driver, an error message appears on the console (refer to screenshot below ...

The shapes of the operands (2,6) and (6,2) do not align for broadcasting, resulting in a ValueError

I am currently attempting to calculate the difference between an identity matrix and an array, then multiply each other with one transposed version of the other. However, I keep encountering this error. X = np.array([[-1, -1], [-2, -1.9], [-3, -2], [1.2, ...

Utilizing Python requests for retrieving dynamic website data

Currently, I am utilizing Python (BeautifulSoup) to extract data from various websites. However, there are instances where accessing search results can be challenging. For example: import requests from bs4 import BeautifulSoup url1 = 'https://auto.r ...

Is there a way to add a pause between typed characters when using .send_keys()?

I've been working on automating an online application and I'm looking for a way to make the ".send_keys()" function more authentic. Instead of inputting "[email protected]" rapidly into the text field, I want to introduce a slight delay betw ...

"Creating a lambda function with multiple conditional statements for data manipulation in a Pand

I have a basic dataset containing revenue and cost values. In my particular case, the cost figures can sometimes be negative. My goal is to calculate the ratio of revenue to cost using the following formula: if ((x['cost'] < 0) & (x[&apo ...