0

I'm trying to change the elements within a form, and the later input options depend on the earlier ones. I can select options using this method for the earlier selector, but for this specific selector it clicks the object (I can see the text change) and then reverts back.

Because of this, I'm fairly certain my id is correct and it, as it does find and select the current element, but I need help in figuring out why the option will not maintain.

 wait = WebDriverWait(driver,20).until(EC.presence_of_element_located(
    (By.XPATH, 'xpath/option[2]')))
select = Select(driver.find_element_by_xpath('xpath'))
select.select_by_value(code)

I've tried selecting by index, visible text, and finding by id and using the click() method but none allow my option selection to be maintained. When I manually go in the form, my option selection is maintained. Furthermore in BeautifulSoup, I can't seem to post to the selector as well.

Would love help exploring why exactly this may be happening! Thanks.

edit: the issue is not that the options are not found. I find the option, and I am able to select the option. When looking at the Form Data within the network headers I can see that I'm attempting to put in the same information as I would manually.

However, less than a second after I select on the option, the selector goes back to its original state.

Auraius
  • 3
  • 3

1 Answers1

0

To interact with the tag you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using select_by_visible_text():

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "id_name")))).select_by_visible_text("option_text")
    
  • Using select_by_value():

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "id_name")))).select_by_value("option_value")
    
  • Using select_by_index():

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "id_name")))).select_by_index(index)
    

Note: You have to add the following imports :

from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks! But the issue is not that the element isn't visible; I do wait. The issue is that after clicking on the selector, the selected option is only temporarily chosen but then reverts back to its original state. I'm not sure why it reverts back. – Auraius Sep 27 '20 at 03:07
  • Hi @DebanjanB, Sorry to track you down here. If you have a moment can you stop by the [dawghaus](https://chat.stackoverflow.com/rooms/169987/dawgs-waffle-haus-) chatroom to perhaps help with a question? You can then ping me from there. – QHarr Oct 04 '20 at 19:18
  • 1
    @QHarr I'm having limited connectivity to stackoverflow due to some unavoidable circumstances. Can we discuss the question/issue/suggestion in the [Selenium Chat Room](https://chat.stackoverflow.com/rooms/223360/selenium) which can be beneficial to other users as well? – undetected Selenium Oct 20 '20 at 18:21