0

I have a similar question that's being worked, but I made an error and I don't think editing the other question makes sense as it requires a complete rewrite and would make the original question (and answers) nonsensical. Apologies for the confusion.

Using Selenium and Python, I am trying to automate downloading of files from a website. I need to make several clicks on different areas of the page in order to kick off the download process.

In my other question I state that I was able to expose the choices in the dropdown menu, but that is not true. I'm not sure why I thought that. So, I need to expose the menu choices first (then I can figure out how to select them - which my other question addresses).

Here is the HTML for the dropdown. I am trying to do the initial click on this dropdown, to expose the choices available; there are four choices displayed when the dropdown is clicked. The choices are constructed using li class (not shown here but I can add them if it helps).

<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">

Here is my code. There are two steps here; the first is to check a checkbox which indicates that I want all displayed documents included in the download (this step works fine) and the second is to display the dropdown menu.

import selenium
from selenium import webdriver

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

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://sample.url")

#this is the long wait to allow time to log in
driver.implicitly_wait(50)

#this selects the checkbox to choose all listed documents. This works correctly.
driver.find_element(By.ID, "SLP-input").click()

#wait for menu to be ready
driver.implicitly_wait(5)

#this is the attempt to display the choices in the dropdown menu. This click is what is not working, even though the dropdown is ready to be clicked. The choices displayed in this dropdown will allow download the documents selected by the checkbox above.
driver.find_element(By.XPATH, "//ul[@class='SLP-dropdown-menu']").click()

Here is the error message:

---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
Cell In[2], line 26
     23 driver.implicitly_wait(5)
     25 #this is the attempt to display the choices in the Download menu. This click is what is not working, even though the dropdown is ready to be clicked
---> 26 driver.find_element(By.XPATH, "//ul[@class='SLP-dropdown-menu']").click()

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":"//ul[@class='SLP-dropdown-menu']"}
  (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:
    0x7ff74a928255
    0x7ff74a9282b0
    0x7ff74a70165d
    0x7ff74a759a33
    0x7ff74a759d3c
    0x7ff74a7adf67
    0x7ff74a7aac97
    0x7ff74a74ac29
    0x7ff74a74ba93
    0x7ff74ac3fff0
    0x7ff74ac3a930
    0x7ff74ac59096
    0x7ff74a945754
    0x7ff74a94e6fc
    0x7ff74a931974
    0x7ff74a931b25
    0x7ff74a917852
    0x7ff96614e8d7
    0x7ff966b4c53c

3
  • This question is similar to: How to select a dropdown built as a button (Selenium/Python). If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented 23 hours ago
  • This is the same question as before... with a minor update. You need to delete this one and go back and edit the first. I've already updated my answer in the first question to address this issue only to find out you've created an entirely new question for the same thing. Commented 23 hours ago
  • The other answer just uses a different element attribute and doesn't address why the code in this question doesn't work. It's fundamentally a different question, just poorly worded, so it sounds similar to the other one. Commented 15 hours ago

2 Answers 2

1

The element you are trying to select has multiple classes:

SLP-dropdown-menu SLP-dropdown-menu-root SLP-dropdown-menu-vertical

... but you are trying to find an element with a single class:

//ul[@class='SLP-dropdown-menu']

To select Any <ul> element that has the class SLP-dropdown-menu, but also works if it has multiple classes defined, you can use this XPath:

//ul[contains(@class, 'SLP-dropdown-menu')

Sign up to request clarification or add additional context in comments.

Comments

1

You're trying to click the <ul> element, but that isn't the clickable part of the dropdown. Most dropdowns are built so the trigger is a wrapper element and the ul only appears in the DOM after the dropdown is opened.

In your HTML snippet, the clickable element is:


<div class="SLP-dropdown SLP-dropdown-placement-bottomLeft" data-qa="SLP-dropdown" style="left: 429px; top: 436px; width: 117px;">

The <ul> becomes visible after you click the dropdown.

so instead of:

driver.find_element(By.XPATH, "//ul[@class='SLP-dropdown-menu']).click()

Click the dropdown wrapper first:

dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.SLP-dropdown[data-qa='SLP-dropdown']"))
dropdown.click()

Once you click the wrapper, the <ul> with the options gets added to the DOM and you can select items normally.

New contributor
Tomax is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.