I'm trying to do log in into my university site but i receive this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div[2]/div/div/div/div[2]/form/table/tbody/tr[3]/td[2]/input"}
this is the code part:
textboxes3 = browser.find_element_by_xpath("/html/body/div/div/div[2]/div/div/div/div[2]/form/table/tbody/tr[3]/td[2]/input")
textboxes3.click()
textboxes3.send_keys(matricola)
and this is the site inspection:1
CodePudding user response:
Try targeting the element by id:
textboxes3 = browser.find_element_by_id("username")
textboxes3.click()
textboxes3.send_keys(matricola)
CodePudding user response:
Using browser.find_element_by_id("username") should work, but since it's still not finding the element then I suspect one of two issues might be occurring:
- The element hasn't loaded yet and selenium needs to wait longer. I recommend using the WebDriverWait class to wait for the element to load.
- Websites are divided into iframes, Selenium may be searching for the element in the wrong iframe. If this is the case you will need to use the driver.switch_to.frame function.
I recommend trying this code out first:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
w = WebDriverWait(self.driver, 10)
textboxes3 = w.until(ec.presence_of_element_located((By.ID, 'username')))
textboxes3.click()
textboxes3.send_keys('matricola')
