0

The element that I'm trying to select is the one below

<div id="mOnlineEligiblity" class="col span_1_of_3" style="">
  <a href="onlineElig.html" style="text-decoration: none !important;color: #275883;"><img src="../../images/mobileHome/Newicon/OnlineEligibility.png" height="65px" width="65px" class="morph">
  <br> Online Eligibility </a>
</div>

I tried the following to select it, but it says element not found

driver.find_element_by_id("mOnlineEligiblity")

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="mOnlineEligiblity"]"}

I want to be able to select and click on it

EDIT: -

It is present inside an iframe like Swaroop mentioned, attaching the image for reference

enter image description here

dpacman
  • 3,683
  • 2
  • 20
  • 35
  • Do you know if it exists when you are running that bit of code? As a hacky test, sleep 5 seconds and then continue. – Cfomodz Mar 30 '22 at 06:52
  • 1
    Might be your element is in the `iframe`. Switch to that `iframe` and then search. – Swaroop Humane Mar 30 '22 at 06:52
  • @Cfomodz yup, tried that too `driver.implicitly_wait(5)`. Still doesn't seem to work – dpacman Mar 30 '22 at 06:56
  • @SwaroopHumane, just checked, its not in an `iframe` – dpacman Mar 30 '22 at 06:57
  • How much time it is taking to load that element ? if it is taking longer time then use Explicit wait. Or the other scenario will be might be some other element overlapping or covering that element. Would be nice if you can share the URL so that we can investigate and help you in better way. – Swaroop Humane Mar 30 '22 at 06:59
  • @SwaroopHumane, the page appears after logging in, so I'm sharing the body element https://syncfiddle.net/fiddle/-MzOceARKAqsOnTVDjt0 – dpacman Mar 30 '22 at 07:04
  • @dpacman Is there any reason that you cannot just use XPATH and target the a tag? Just curious. I know you *should* be able to do what you are trying, but if there is some issue, I am trying to just get you want you want instead of answering your question, if that makes sense – Cfomodz Mar 30 '22 at 07:11
  • @Cfomodz I tried XPATH too, throws the same error – dpacman Mar 30 '22 at 07:22
  • @dpacman Oh now you've piqued my interest. Can you post more code so I can replicate this, please? – Cfomodz Mar 30 '22 at 07:25
  • @Cfomodz, haha. Just double checked, it is inside an `iframe` like Swaroop mentioned, unable to copy it, attaching the image in the question – dpacman Mar 30 '22 at 07:34
  • 2
    Okay then switch to that `iframe` and then search for this element, you will definitely find it. Cheers..!! This [Thread](https://stackoverflow.com/questions/44834358/switch-to-an-iframe-through-selenium-and-python) might help you. – Swaroop Humane Mar 30 '22 at 07:39
  • 1
    @Cfomodz, and Swaroop thank you for the help, I was able to solve it :) – dpacman Mar 30 '22 at 07:39
  • 1
    @dpacman Well done and glad you got it! Swaroop, nice intuition on what was going on - he had the answer for your first try ;) – Cfomodz Mar 30 '22 at 07:54

2 Answers2

1

Had to switch to the iframe like Swaroop mentioned.

# Find the iframe
iframe = driver.find_element_by_xpath("//iframe[@name='NAME_OF_THE_FRAME']")

# Switch to the iframe
driver.switch_to.frame(iframe)

# Now find the element
driver.find_element_by_id("mOnlineEligiblity").click()
dpacman
  • 3,683
  • 2
  • 20
  • 35
1

As long as it is indeed on the page that you are loading (isn't in an iframe), then you shouldn't have any issue locating it and clicking on it.

import platform
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options

options = webdriver.ChromeOptions()
if platform == "linux" or platform == "linux2":
    driver = webdriver.Chrome(executable_path='chromedriver_linux', chrome_options=options)
elif platform == "darwin":
    driver = webdriver.Chrome(executable_path='chromedriver_mac', chrome_options=options)
else:
    driver = webdriver.Firefox()

    driver.maximize_window()

    timeout = 10

    driver.get("file:///G:/Downloads/random.html")

    xpath = '//*[@id="mOnlineEligiblity"]/a'
    WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.XPATH, xpath)))

    link_to_click = driver.find_element(By.XPATH, XPath)

    link_to_click.click()

    driver.quit()

I would still suggest targeting the element that you actually want to click on via the xpath = '//*[@id="mOnlineEligiblity"]/a' instead of just xpath = '//*[@id="mOnlineEligiblity"]' but that's more of a best practice than anything you need to worry about if it's a one-off script.

Cfomodz
  • 532
  • 4
  • 17