0

HTML of the website

The HTML code of the element I want to select:

   <a href="../s-reminderNotice.asp?fname=b%2D3c%2DpLessonBooking%2Easp%3Flimit%3Dpl" class="sidelinkbold" 
   target="mainFrame" 
   onmouseover=" window.status='Practical Training Booking'; 
   return true" onmouseout="window.status=' ';
   return true">Booking without Fixed Instructor</a>

I would like to select the element of this "Booking without Fixed Instructor" ; however there isn't an ID/link_text/name for this element. How can I direct the site to the HREF. Is there a way to use xpath to locate it , the element is in the column on the left of the website so there are many other elements of the same class. (see pic.)

I tried this and it returned:

AttributeError: 'WebDriver' object has no attribute 'findElement'

Code:

driver.findElement(By.xpath("//a[@href='THE LINK']")).click();

SOLVED; ELEMENT WAS IN AN IFRAME ; HAD TO SWITCH FRAME TO ACCESS ELEMENT

3 Answers3

1
AttributeError: 'WebDriver' object has no attribute 'findElement'

you are using python so driver dowsn't have findElemnt method , it is find_Element

https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html?highlight=webdriver%20class#selenium.webdriver.remote.webdriver.WebDriver.find_element_by_class_name

use:

    driver.find_element(By.XPATH("//a[contains(string(),'Booking without Fixed Instructor')]")
PDHide
  • 18,113
  • 2
  • 31
  • 46
0

To click on the element with text as Booking without Fixed Instructor you can use either of the following Locator Strategies:

  • Using link_text:

    driver.find_element(By.LINK_TEXT, "Booking without Fixed Instructor")
    
  • Using partial_link_text:

    driver.find_element(By.PARTIAL_LINK_TEXT, "Booking without Fixed Instructor").click()
    
  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "a.sidelinkbold[href*='s-reminderNotice']").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//a[@class='sidelinkbold' and contains(@href, 's-reminderNotice')][contains(., 'Booking without Fixed Instructor')]").click()
    

The desired element is a dynamic element, so ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Booking without Fixed Instructor"))).click()
    
  • Using PARTIAL_LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Booking without Fixed Instructor"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.sidelinkbold[href*='s-reminderNotice']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='sidelinkbold' and contains(@href, 's-reminderNotice')][contains(., 'Booking without Fixed Instructor')]"))).click()
    
  • Note: You have to add the following imports :

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

References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • This is perfect however I have tried all the methods but still get NoSuchElementException ; I have already added the wait buffer to allow the website to load? and reason this might still occur – gabethechef Mar 24 '21 at 12:26
  • I have figured it out ; apparently it was under an iframe , thus i had to switch the frame to access the element – gabethechef Mar 25 '21 at 07:07
  • @gabethechef Updated the answer with references to **NoSuchElementException** – undetected Selenium Mar 25 '21 at 14:24
-1

Try this xpath:

//a[text()[contains(.,'Booking without Fixed Instructor')]]
nkpydev
  • 11
  • 4