0

I'm having an issue using Selenium on Python. Here is the whole error code :

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[3]/div[2]/form[1]/input"}
  (Session info: MicrosoftEdge=89.0.774.57)

and here is the element I'm trying to access :

enter image description here

The fact is that when I click on that element and copy the full XPath, here is what I have :

/html/body/div/div[3]/div[2]/form[1]/input

but it doesn't work for some reasons... I tried almost everything : with the name, with the value, with the normal XPath (which is //*[@id="content"]/form[1]/input by the way)

I'm using Edge Chromium and I use Python 3.9.0 and here is the code I'm using (everything work perfectly until the variable telechargerReleve :

from selenium import webdriver

driver = webdriver.Edge(executable_path="automation_drivers/msedgedriver.exe")
driver.get("https://login.unice.fr/login?service=https://ent.unice.fr/uPortal/Login")

usernameElement = driver.find_element_by_id("username")
passwordElement = driver.find_element_by_id("password")

usernameElement.send_keys("myUsername")
passwordElement.send_keys("myPassword")

login = driver.find_element_by_name("submit")
login.click()

mesInfos = driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/td[3]/table/tbody/tr[2]/td[3]/a")
mesInfos.click()

intracursus = driver.find_element_by_xpath("/html/body/table[3]/tbody/tr/td[2]/span[2]/a[3]")
intracursus.click()

telechargerReleve = driver.find_element_by_xpath("/html/body/div/div[3]/div[2]/form[1]/input")
telechargerReleve.click()

Thank you in advance, Roux.

PS : Some texts may be in French so if you need a traduction or something, feel free to ask me!

Roux
  • 69
  • 1
  • 8
  • use a more targetted XPATH. Maybe by the text of the button. (This is also how the user would find it...) Seems like it's the first form, there... but path based xpath's are brittle. Depending on how the site works it may also need a webdriverwait... – pcalkins Mar 22 '21 at 19:01
  • ALl xpath based locators are NOT brittle. This is a fallacy. However some xpath can be brittle. – DMart Mar 22 '21 at 20:33
  • Assuming you are wanting that element that is highlighted in the HTML you should use find element by name. It's available to use. Make it easier on yourself – JD2775 Mar 22 '21 at 20:51
  • It seems you clicked an a tag prior to finding that element. Use a wait. – Arundeep Chohan Mar 22 '21 at 23:51
  • any change to the HTML (development or a Javascript update) can change the path to your webelement. Since you are using a purely path-based XPATH there, it is brittle. You could end up with another webelement because the path to the one you want has changed. So it's not only brittle it's also not targetted to a particular element of the DOM, but rather a path that can change with updates. – pcalkins Mar 23 '21 at 19:38
  • I also tried with a Wait and a tag prior to find that element but unfortunately it doesn't work... I get a TimeoutException – Roux Mar 23 '21 at 20:54
  • I think it's related to iFrame or something like that – Roux Mar 24 '21 at 15:22

2 Answers2

1

Okay so I figured out what was the issue. The website used an iframe tag in the HTML for the button which was obviously the main reason why I couldn't click on it. What I edited in my code was the last 2 lines which I replaced with :

seq = driver.find_elements_by_tag_name('iframe')

for index in range(len(seq)):   
    driver.switch_to_default_content()
    iframe = driver.find_elements_by_tag_name('iframe')[index]
    driver.switch_to.frame(iframe)
    try:
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME,"telrelevepresences"))).click()
    except TimeoutException:
        pass

Thank you everyone for your answers !

Roux
  • 69
  • 1
  • 8
0

Induce a wait after your click the a tag.

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME,"telrelevepresences"))).click()

Import

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32