I am trying to select choices from a dropdown menu, using Selenium with Python. The menu might be built as a button as I had no luck trying to use Select. It also might be a Bootstrap dropdown. Here is an excerpt of what I see when I use Inspect on Chrome. The “Choice 4” selection near the end of the code is the menu choice I would like to make. This is a webpage that is not publicly accessible.
<div class="SLP-dropdown SLP-dropdown-placement-bottomLeft" data-qa="SLP-dropdown" style="left: 429px; top: 436px; width: 117px;">
<ul class="SLP-dropdown-menu SLP-dropdown-menu-root SLP-dropdown-menu-vertical" role-“menu" tabindex="0">
<li class="SLP-dropdown-menu-item" role="menuitem">Choice 1</li>
<li class="SLP-dropdown-menu-item" role="menuitem">Choice 2</li>
<li class="SLP-dropdown-menu-item" role="menuitem">Choice 3</li>
<li class="SLP-dropdown-menu-item" role="menuitem">Choice 4</li>
Here is my current code. I can get the dropdown to display the four selections on the screen using the "SLP-input" ID, but it won't let me select any of the four selections.
import selenium
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://sample URL")
driver.find_element(By.ID, "SLP-input").click()
driver.find_element(By.XPATH, "//select/option[contains(text(),'Choice 4')]").click()
Here is the error:
---------------------------------------------------------------------------
NoSuchElementException Traceback (most recent call last)
Cell In[30], line 28
24 #driver.find_element(By.XPATH, "//span[contains(text(),'dummy')]").click()
26 driver.find_element(By.ID, "ag-105-input").click()
---> 28 driver.find_element(By.XPATH, "//select/option[contains(text(),'Choice 4')]").click()
30 #driver.find_element(By.XPATH, "//li[contains(text(),'Choice 4')]").click()
31 #driver.find_element(By.XPATH, "//li[text()='Choice4rmats']").click()
32
(...) 80
81 #driver.quit()
File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\remote\webdriver.py:926, in WebDriver.find_element(self, by, value)
923 raise NoSuchElementException(f"Cannot locate relative element with: {by.root}")
924 return elements[0]
--> 926 return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\remote\webdriver.py:458, in WebDriver.execute(self, driver_command, params)
455 response = cast(RemoteConnection, self.command_executor).execute(driver_command, params)
457 if response:
--> 458 self.error_handler.check_response(response)
459 response["value"] = self._unwrap_value(response.get("value", None))
460 return response
File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\remote\errorhandler.py:232, in ErrorHandler.check_response(self, response)
230 alert_text = value["alert"].get("text")
231 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here
--> 232 raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//select/option[contains(text(),'All formats')]"}
(Session info: chrome=143.0.7499.41); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#nosuchelementexception
Stacktrace:
Symbols not available. Dumping unresolved backtrace:
0x7ff7ccbe8255
0x7ff7ccbe82b0
0x7ff7cc9c165d
0x7ff7cca19a33
0x7ff7cca19d3c
0x7ff7cca6df67
0x7ff7cca6ac97
0x7ff7cca0ac29
0x7ff7cca0ba93
0x7ff7cceffff0
0x7ff7ccefa930
0x7ff7ccf19096
0x7ff7ccc05754
0x7ff7ccc0e6fc
0x7ff7ccbf1974
0x7ff7ccbf1b25
0x7ff7ccbd7852
0x7ffccbe5e8d7
0x7ffccd0ac53c