Home > Mobile >  Issues locating elements in Selenium
Issues locating elements in Selenium

Time:08-27

I am just getting started with Selenium and I am having some issues trying to locate different elements on a particular website. The website I want to crawl is https://connect.garmin.com/signin since they unfortunately do not provide API access to individuals.

In any case, the element I am trying to "find" is or the login box. I am having much issues with that however, and I do not know what I am doing wrong at this point.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = browser=webdriver.Firefox()
driver.get("https://connect.garmin.com/signin")

element = driver.find_element("name", "username")

element.clear()
element.send_keys("[email protected]")

This is my current code.

Traceback (most recent call last):
  File "/Users/will/PycharmProjects/GarminCrawlerTwitter/Crawler.py", line 7, in <module>
    element = driver.find_element("name", "username")
  File "/Users/will/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 855, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/Users/will/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in execute
    self.error_handler.check_response(response)
  File "/Users/will/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="username"]
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:188:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:400:5
element.find/</<@chrome://remote/content/marionette/element.js:292:16

And that is the error message. It seems to run into issues trying to find the element "username". Tried using XPATH //[@id="username"] and //[@name="username"] but with same results.

I have also used driver.implicitly_wait(3) thinking that it might have been that the site wasn't fully loaded before trying to locate the username element, and that was why it was failing but that seems to have been incorrect as well.

CodePudding user response:

@Zeno, the login pop-up is inside an iframe so first, you need to switch to that frame. The following should work.

driver.get("https://connect.garmin.com/signin")
iframe = driver.find_element(By.ID, "gauth-widget-frame-gauth-widget")
driver.switch_to.frame(iframe)
element = driver.find_element("name", "username")

element.clear()
element.send_keys("[email protected]")

you will need the following import as I have used By:

from selenium.webdriver.common.by import By
  • Related