2

I'm using Selenium with Python 3.5 to automate a process I do on a regular basis. However, I need to invoke a drop-down list by clicking a button on the header of the website. I've tried so many times to make this thing work but this is frustrating. I've tried to find the element by "ID","class", and directly copying the xpath into the code. I'm fairly new to this so any help is greatly appreciated! The truncated version of my code is:

SwitchOffices = driver.find_element_by_xpath('//*[@id="tdSwitchOff"]/a').click()

Here is the HTML code that I've highlighted on the selected item that will trigger the drop-down list:

<a class="BannerMessage" style="cursor: pointer; color: blue;
text-decoration: none;" onclick="JavaScript:fnShowBUList();" 
onmouseout="this.style.textDecoration='none';"
onmouseover="this.style.textDecoration='underline';">
                                    Switch Offices </a>

Not sure if this matters but this is the HTML code that is above the xpath that I want to put in my code.

<div id="tdSwitchOff" valign="top" style="float: right; visibility:
visible; display: inline;">
&nbsp;<a class="BannerMessage"
style="cursor: pointer; color: blue; text-decoration: none;" 
onclick="JavaScript:fnShowBUList();" onmouseout="this.style.textDecoration='none';"
onmouseover="this.style.textDecoration='underline';">
                                    Switch Offices </a> |
</div>

Running my Selenium code against the HTML provided produces a NoSuchElementException.

3
  • What happens when you run your Selenium code you provided? Is there a NoSuchElementException, or does nothing happen at all? Knowing what the error message is (if any) will help track down the issue. Commented Nov 20, 2019 at 18:47
  • Sorry about that @Christine! I get the following error: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="tdSwitchOff"]/a"} Commented Nov 20, 2019 at 18:51
  • No worries, thanks for the update. Your XPath looks correct to me. Let me see if I can suggest a few alternative paths in an answer. Commented Nov 20, 2019 at 18:56

1 Answer 1

1

I don't see anything blatantly wrong with your XPath, but trying a different locator strategy never hurts -- you could try updating your locator to look directly for the a element, rather than going through the div first. I also invoke WebDriverWait so that we can ensure we are waiting for the element to exist before clicking it:

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


# wait for dropdown to exist
SwitchOffices = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//a[contains(text(), 'Switch Offices')]")))

# click dropdown
SwitchOffices.click()

If this does not work, there may be some other issues on the web page itself -- to further troubleshoot, we may need to see the full page HTML, or a link to the page you are automating.

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

2 Comments

Thanks @Christine for helping me with this! I recieved a "TimeoutException" message when attempting the code provided. I think the issue is that the site that I'm working on is a web-portal/intranet site. That could be the reason why I'm encountering these issues.
If the TimeoutException is occurring, you could try increasing the wait time in WebDriverWait(driver, 10) to 20 instead of 10. The other possibility is that the locator strategy is not correct, possibly due to a hidden iframe element on the page. If you'd like, you can provide me the link to the page you are automating and I can take a closer look at what the issue might be.

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.