I can't find element in Selenium by id, name or link text or WebDriverWait(). A "No such error" exception is thrown every time

Question:

Consider:

<a name="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" id="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" ptlinktgt="pt_peoplecode" tabindex="126" href="javascript:submitAction_win0(document.win0,'DERIVED_SSS_SCL_SSS_ENRL_CART$276$');" class="SSSHYPERLINKBOLDSMALL">Enrollment Shopping Cart</a>

Is the element I want to find.

Background: I’m trying to automate class registration. The page in question must be accessed by some security measures and is definitely not a typical web page.

First, I tried this:

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

driver = webdriver.Chrome()
username = input("Username: ")
password = input("Password: ")

username_box = driver.find_element_by_name("j_username")
username_box.send_keys(username)

password_box = driver.find_element_by_name("j_password")
password_box.send_keys(password)
password_box.send_keys(Keys.RETURN) # Now, we are at the page in question after signing on.

I tried this:

to_shopping_cart = driver.find_element_by_link_text("Enrollment Shopping Cart")

with find_element_by_link_text, find_element_by_partial_link_text, find_element_by_id, find_element_by_name and a few other options listed in the documentation on 7. WebDriver API. All of these threw "no such element".

Next, I tried using WebDriverWait. This kept throwing "TimeOut exception".

Current theories:

  1. It has to do with the protected sign in. It’s a really strangely built site and is not intuitive at all. There are tons and tons of elements within one another.

  2. It has to do with the other elements around it. I’ll definitely upload those if I need to. It’s just a huge block of HTML, so I wanted to keep the question as concise as possible for the time being.

  3. I thought about using driver.get() with the href action, but that just reloads the web page. So that doesn’t work either.


I solved the problem

By switching to the proper iFrame with:

proper_frame_element = driver.find_element_by_name('TargetContent')
driver.switch_to_frame(proper_frame_element)
Asked By: Derek Fulton

||

Answers:

Try to capture the elements through their XPath expression. XPath expressions seems to usually work well for me.

To easily find the XPath expression, rather than entering it in yourself, you can go to the website through Chrome, right click on the element you want the XPath expression for, select inspect element and a sidebar of the page HTML will pop out.

In the sidebar, there should be a line highlighted in grey. That’s the line in the HTML content that is pointing to the element and if you mouse over that line, you should see your element highlighted. Right click on that line and select Copy XPath and you should have your XPath expression Put this into your code:

driver.find_element_by_xpath("xpath_of_element_you_want").sendkeys("your_username")

Make sure your XPath expression is in quotes (a string) and that the username and password keys you want to send are also strings.

Answered By: Jonathan

According the information given in comments, the desired element is inside an iframe. This means that you need to switch to the iframe before making a search:

driver.switch_to.frame("frame name or id")
to_shopping_cart = driver.find_element_by_link_text("Enrollment Shopping Cart")

Then, if you need to switch to a default content, use:

driver.switch_to.default_content()
Answered By: alecxe