简体   繁体   中英

NoSuchElementException when trying to use Selenium Python

I keep getting a NoSuchElementException when trying to use Selenium to find an element in python. I'm waiting for the page to fully load, and I'm switching to the right frame (or at least I think so!).

Here is the code:

driver.get("https://www.arcgis.com/home/signin.html")
driver.implicitly_wait(10)


driver.switch_to_frame("oAuthFrame")
elem = driver.find_element_by_name('username')
elem1 = driver.find_element_by_name('password')

Here is the webpage part I'm trying to access:

<input id="user_username" class="textBox" type="text" name="username" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">

which is located inside

<iframe dojoattachpoint="_iFrame" id="oAuthFrame" scrolling="no" style="display: block; border: 0px;" marginheight="0" marginwidth="0" frameborder="0" width="400" height="500"...>

You can go see the source code for yourself at https://www.arcgis.com/home/signin.html

Full error output:

    Traceback (most recent call last):
  File "C:\Python34\beginSample.py", line 12, in <module>
elem = driver.find_element_by_name('username')
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 302, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 662, in find_element
{'using': by, 'value': value})['value']
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 173, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 164, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: 'no such element\n
(Session info: chrome=35.0.1916.153)\n  (Driver info: chromedriver=2.9.248315,pl
atform=Windows NT 6.1 SP1 x86_64)'

If someone could help me figure out what's wrong, I'd greatly appreciate it.

UPDATE : I'm now using actions, and I've debugged to the point of no errors, but also its not typing anything. Here is the code:

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

actions = ActionChains(driver)
actions.send_keys("sd")
actions.send_keys(Keys.TAB)
actions.send_keys("bg")
actions.perform()

Personally I try to stick with the xpath and utilize xquery functionality. You can still use the other attachments since they are usually "attributes" of the element, but it provides more flexibility for complex types contains searching. I am able to find the username/password fields with the below two attach xpaths. You shouldn't need to switch to the iframe first...although some of the direct calls like name seem to not function...the xpath version worked for me.

    element = driver.find_element_by_xpath("//input[@name='username']")
    element1 = driver.find_element_by_xpath("//input[@name='password']")

Update : I was able to duplicate the issue... I'm not sure why the locators are not functioning for this specifically, but here is a work around that works. The default placement of the focus when the page is loaded is the username box.

    actions = selenium.webdriver.common.action_chains.ActionChains(driver)
    actions.SendKeys("sd").Perform()
    actions.SendKeys(selenium.webdriver.common.Keys.Tab).Perform()
    actions.SendKeys("bg").Perform()

http://selenium.googlecode.com/git/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html I'll update again if I can figure out a way to directly connect to the fields.

Update: With Chrome if you grab the specific iFrame you will see an error that says your browser does not support this iFrame. I believe the communication breakdown might be in the browser not being able to read it correctly...even though it seems to render it and allow functionality correctly. Since Selenium is based on the DOM selection it would be attempting to utilize similar behaviors to find this. I would recommend trying a driver.execute_script("script goes here") and trying to locate it purely with javascript and see if that would work. You would of course then have to continue using javascript and modify the control attributes/properties directly with javascript and then execute a javascript submit event for the button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM