1

I'm trying to select the text of the menu option circled in the screen shot. In this case it's 'Age Harden'.

The best I can get is the selected value of 17043, not the text 'Age Harden'.

Here's what I've tried:

driver.find_element_by_xpath("//select[@id='lstOperation_Key']").get_attribute('text_content')

Returns None

driver.find_element_by_xpath("//select[@id='lstOperation_Key']").get_attribute('value')

Returns 17043

driver.find_element_by_xpath("//select[@id='lstOperation_Key']").get_attribute('text')

Returns None

driver.find_element_by_xpath("//select[@id='lstOperation_Key']").get_attribute('selected value')

Returns None

my_furnace_parameters_data['Furnace_Operation'] = driver.find_element_by_xpath("//select[@id='lstOperation_Key']//option[1]").get_attribute('text')

Returns 'Age Harden', BUT, when I test this on an operation that is not the 1st one on the list like 'Zoo Treatment' is it fails - it still returns 'Age Harden'.

Any help or pointers is appreciated, thank you! enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Programming_Learner_DK
  • 1,509
  • 4
  • 23
  • 49

3 Answers3

3

Use Select class:

from selenium.webdriver.support.select import Select

operation_key = Select(driver.find_element_by_id('lstOperation_Key'))
operation_key.select_by_visible_text('Age Harden')
# operation_key.select_by_value('17043')
# operation_key.select_by_index(1)

You can use WebDriverWait to wait for the element to be accessible:

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

wait = WebDriverWait(driver, 10)
operation_key = Select(wait.until(EC.element_to_be_clickable((By.ID, 'lstOperation_Key'))))
operation_key.select_by_visible_text('Age Harden')
# operation_key.select_by_value('17043')
# operation_key.select_by_index(1)
Sers
  • 12,047
  • 2
  • 12
  • 31
  • when I use these I keep getting a WebDriverObject back, such as: `` . I tried to add `.get_attribute('value')` to get the text but it threw an error: `AttributeError: 'Select' object has no attribute 'get_attribute'` - any idea how I can get the value and not an object? – Programming_Learner_DK Feb 03 '20 at 19:28
  • Here's the line of code I am trying to assign to a key in my dictionary: `Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'lstOperation_Key')))).get_attribute('value')` – Programming_Learner_DK Feb 03 '20 at 19:30
  • Are try to select or get selected text? There's no .get_attribute('value') in my code and it will return None, because `Select` class does not have `get_attribute` method – Sers Feb 03 '20 at 20:03
3

first_selected_option

first_selected_option() returns the first selected option in this select tag (or the currently selected option in a normal select).


Seems you were pretty close. To extract the textContent of the default selected <option> you can use the first_selected_option property to identify the element and you can extract the option text as per the solution below:

  • Code Block:

    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='lstOperation_Key' and @name='lstOperation_Key']")))) //selecting tag
    element = select.first_selected_option
    print(element.text)
    # or
    print(element.get_attribute("innerHTML"))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

use can also use this code

location_text = ""
location_texts = driver.find_element(By.ID,  "select id").find_elements(By.TAG_NAME, "option")
for ii in location_texts:
if str(location_value) == str(ii.get_attribute("value")):
    location_text = ii.text
BugsCreator
  • 433
  • 3
  • 10