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
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
You can find a couple of relevant discussions on NoSuchElementException in: