Home > other >  Unable to find element in Chrome browser
Unable to find element in Chrome browser

Time:10-18

I'm trying to learn Selenium. The code below should open fastmail in Chrome and allow me to search for the specified web element.

I can get this to work in the Python interpreter by entering the code line by line.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.fastmail.com/login/")

username = driver.find_element(By.ID, 'v17-input')

However, when I try to run it as a full Python script I am unable to find the element I am looking for. The error message is below.

Traceback (most recent call last):
  File "C:\MyPythonScripts\test.py", line 9, in <module>
    username = driver.find_element(By.ID, 'v17-input')
  File "C:\Users\slong\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 856, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\slong\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute
    self.error_handler.check_response(response)
  File "C:\Users\slong\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="v17-input"]"}
  (Session info: chrome=106.0.5249.119)
Stacktrace:
Backtrace:
    Ordinal0 [0x00801ED3 2236115]
    Ordinal0 [0x007992F1 1807089]
    Ordinal0 [0x006A66FD 812797]
    Ordinal0 [0x006D55DF 1005023]
    Ordinal0 [0x006D57CB 1005515]
    Ordinal0 [0x00707632 1209906]
    Ordinal0 [0x006F1AD4 1120980]
    Ordinal0 [0x007059E2 1202658]
    Ordinal0 [0x006F18A6 1120422]
    Ordinal0 [0x006CA73D 960317]
    Ordinal0 [0x006CB71F 964383]
    GetHandleVerifier [0x00AAE7E2 2743074]
    GetHandleVerifier [0x00AA08D4 2685972]
    GetHandleVerifier [0x00892BAA 532202]
    GetHandleVerifier [0x00891990 527568]
    Ordinal0 [0x007A080C 1837068]
    Ordinal0 [0x007A4CD8 1854680]
    Ordinal0 [0x007A4DC5 1854917]
    Ordinal0 [0x007AED64 1895780]
    BaseThreadInitThunk [0x76926739 25]
    RtlGetFullPathName_UEx [0x777A8FD2 1218]
    RtlGetFullPathName_UEx [0x777A8F9D 1165]

And I have not clue what it's about. The error message seems to imply that it is unable to locate the element.

CodePudding user response:

I see 2 problems here:

  1. You need to add a delay to wait for element's clickability. WebDriverWait expected_conditions is the right tool for this.
  2. You are using a wrong locator.

This code worked:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://www.fastmail.com/login/'
driver.get(url)
wait = WebDriverWait(driver, 10)

username_field = wait.until(EC.element_to_be_clickable((By.ID, "v16-input")))
password_field = wait.until(EC.element_to_be_clickable((By.ID, "v17-input")))
  • Related