-2

I have a link I need to click on:

<a id="selectLink">...</a>

I do it like so:

WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((By.ID, "selectLink")))

but for some reason the link doesnt click, or it does and nothing happens. When I do it manually it works. I even try to put it in a loop and click on it until something happens, but then it works at times and sometimes it doesn't:

while True:
    try:
        WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((By.ID, "selectLink"))).click()
    except Exception:
        break

I can't tell what is the problem.

For example:

while True:
    try:
        WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((By.ID, "selectLink"))).click()
        print(len(browser.find_elements_by_id("selectLink")))
        print('click')
    except Exception:
        print(len(browser.find_elements_by_id("selectLink")))
        print('break')
        break

It gives me:

1 
click
1 
click
1
break

And still nothing happens. My question is how come the loop breaks even tho the link is still accessible, since the length is still 1?

Meryem
  • 477
  • 7
  • 17

3 Answers3

1

I think you were pretty close. Let us see what went wrong.

  • As per your code first code trial:

    WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((By.ID, "selectLink")))
    

In this attempt you have considered the ID attribute of the element where as the element is a <a> tag. As a general practice when you need to interact with a <a> tag is it always a better idea to take help of the linkText present within the <a> tag.

  • As per your code second code trial:

    WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((By.ID, "aaaa.Isu_Recherche_de_prmAvanceView.Button_Selectionner"))).click()
    

In this attempt the ID attribute which you have considered doesn't matches the ID of the element as per the HTML.

Hence you see errors.

Solution

As per the HTML you have shared to click on the desired element you need to induce WebDriverWait for the element to be clickable and can use either of the solutions:

  • LINK_TEXT:

    WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((By.LINK_TEXT, "Sélectionner"))).click()
    
  • PARTIAL_LINK_TEXT:

    WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Sélectionner"))).click()
    
  • CSS_SELECTOR:

    WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.urBtnCntTxt"))).click()
    
  • XPATH:

    WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='urBtnCntTxt'][contains(.,'Sélectionner')]"))).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
    
  • While using CSS_SELECTOR or XPATH it is always better to construct them with the help of minimum two attributes amongst class, id, or others

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I'm sorry, I actually made a mistake in my original post, and the IDs in both trials I made are the same. I have tried the other suggestions as well and like my trial sometime it works sometime it doesn't.. – Meryem Jul 06 '18 at 12:45
  • That's what I was trying actually and just like my original try, at times it succeeds to click and other times it doesn't – Meryem Jul 06 '18 at 12:59
  • Try out my updated answer and let me know the status – undetected Selenium Jul 06 '18 at 12:59
  • Still doesn't work. I updated my post with an explanation – Meryem Jul 06 '18 at 13:11
0

You can try something like this :

button = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "text between anchor tags")))
button.click()
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

If I were to guess, it might be a timing issue. If you click the link, the link still exists on the page since the locator is the same. If you instead click the link, wait for the element to go stale (which indicates the page is reloading), then wait for it to be clickable (indicating the page has finished reloading), then click it again... that loop might work.

wait = WebDriverWait(browser, timeout)
while driver.find_elements_by_id("selectLink")
    link = wait.until(EC.element_to_be_clickable((By.ID, "selectLink")))
    link.click()
    wait.until(EC.staleness_of(link))
JeffC
  • 22,180
  • 5
  • 32
  • 55