1

I've been tinkering with Selenium (Python) for the day working on a project. I'm stuck trying to click on a link of the webpage because I'm unable to retrieve the element by any of the ways I've found until now.

The link is this:

<p>
<a href="0_fr.asp?contenido=0actunor.asp" target="mainFrame"><font size="3" face="Arial" color="#CC9900">
<b>Base de datos de Disposiciones Vigentes</b></font></a>
</font>
</p>

Now, I've tried these things so far, and all give me NoSuchElementException:

  1. driver.find_element_by_css_selector("a[href=0_fr.asp?contenido=0actunor.asp]")
  2. driver.find_element_by_css_selector('div:contains("Base de datos de Disposiciones Vigentes")')
  3. same as 1) but copying and pasting CSS selector alone as a String
  4. by link text and partial link text using the content between and
  5. by using id just in case it does something.

And several more found across other StackOverflow questions and online guides to build my own css selectors and such, but I don't know any HTML or CSS so it's been a bit difficult to follow them. I can provide more information if needed, but i'm unable to link the webpage as you would have to log in and it's a job thing.

Any help or places to search for more information are greatly welcome!

SOLVED: The problem was that the link was inside a frame, which was inside another frame. I just hade to use driver.switch_to.frame("frameName") twice, then use css_selector normally and works as it should.

Alberefe
  • 27
  • 5

2 Answers2

0

Try the following command.

driver.find_element_by_link_text("Base de datos de Disposiciones Vigentes")

Storm Claw
  • 59
  • 2
  • 8
0

The element is <a> element and by all possible means you will interact i.e. click on it. Ideally to invoke click() on any element using Selenium you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='contenido'][href*='actunor'][target='mainFrame'] b"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, 'contenido') and contains(@href, 'actunor')][@target='mainFrame']//b[text()='Base de datos de Disposiciones Vigentes']"))).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
    

Update

As you are observing TimeoutException inducing WebDriverWait you need to address NoSuchElementException first following the discussion in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi! Thanks for the answer. Now instead of giving the NoSuchElement Exception it's returning "selenium.common.exceptions.TimeoutException" with no message. it is a step forward i suppose! – Alberefe May 28 '20 at 17:50
  • @Alberefe Checkout the answer update and let me know the status. – undetected Selenium May 28 '20 at 20:01
  • 1
    Hi, @DebanjanB ! Thanks for the sources! After trying for a while and reading the links you gave me i found the problem. The link was inside a frame which was at the same time inside another frame. I just had to switch.frame() twice, then click on it by css_selector normally. Thanks a lot for the help and have a nice day! – Alberefe May 29 '20 at 11:58