Using webdriver cannot locate input field even though visible

1,745 views
Skip to first unread message

ron4563

unread,
Oct 18, 2011, 3:06:05 PM10/18/11
to Selenium Users
Hi everyone,

I tried a recent post and got no response. Hope that I have better
luck this time.
I am new to selenium and am using Yahoo Mail as my practice
application by trying to add a New Contact to my Yahoo email account.
When you get into Yahoo mail, you can then click a Contact tab which
makes a New Contact button visible ( I am assuming that this is done
by Ajax). When I run the test in webdriver, I can see the Contact tab
being clicked and am able to click the New Contact button which makes
all the input fields visible for the contact information. However when
I attempt to enter data in the first field called Nickname, selenium
is not able to find the field using xpath. I was able to find the
xpath fields using the Xpather tool.

Is it possible that the fields are in a different page or frame? If
so, I am not sure how to specify the new page or new frame.


Can someone with more experience please tell me what I am doing wrong.
I would very much appreciate any help.

Below is the selenium test case that was exported to Python:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
import unittest, time, re

class YahooCreateContact1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.yahoo.com/"
self.verificationErrors = []

def test_yahoo_create_contact1(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_xpath("//li[@id='pa-u_14782488-bd']/a/
span[2]").click()
driver.find_element_by_id("username").clear()

driver.find_element_by_id("username").send_keys("xxx...@yahoo.com")
driver.find_element_by_id("passwd").clear()
driver.find_element_by_id("passwd").send_keys("yyyyyyyyy")
driver.find_element_by_id(".save").click()
# Click on the Contacts tab
driver.find_element_by_xpath("//a[@id='tabcontacts']/
b").click()
time.sleep(5)
# driver.find_element_by_link_text("New Contact").click()
# Wait until the New Contact button is visible
for i in range(60):
try:
if driver.find_element_by_xpath("//*[@id='toolbar']/
div[1]/span[1]/a").is_displayed(): break
except: pass
time.sleep(1)
else: self.fail("time out" )
driver.find_element_by_xpath("//*[@id='toolbar']/div[1]/
span[1]/a").click()
print "New Contact has been clicked"
# Wait for the Nick Name field to be visible
for i in range(60):
try:
if driver.find_element_by_xpath("//
*[@id='nickname:nickname:']/div/input").is_displayed(): break
except: pass
time.sleep(1)
else: self.fail("time out" )
driver.find_element_by_xpath("//*[@id='nickname:nickname:']/
div/input").clear()
driver.find_element_by_xpath("//*[@id='nickname:nickname:']/
div/input").send_keys("Jimmy")
driver.find_element_by_xpath("//*[@id='email:email:']/
input").clear()
driver.find_element_by_xpath("//*[@id='email:email:']/
input").send_keys("jwi...@hotmail.com")
driver.find_element_by_xpath("//*[@id='phone:phone:']/
input").clear()
driver.find_element_by_xpath("//*[@id='phone:phone:']/
input").send_keys("908-668-1368")
driver.find_element_by_xpath("//*[@id='saveAction']/
a").click()

def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True

def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
unittest.main()

Here is the output from the test execution itself from the python
shell:

New Contact has been clicked
E
======================================================================
ERROR: test_yahoo_create_contact1 (__main__.YahooCreateContact1)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\PythonPrograms\SeleniumTestCases
\YahooCreateContact.py", line 37, in test_yahoo_create_contact1
driver.find_element_by_xpath("//*[@id='nickname:nickname:']/div/
input").clear()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote
\webdriver.py", line 190, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote
\webdriver.py", line 525, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote
\webdriver.py", line 144, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote
\errorhandler.py", line 118, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element:
{"method":"xpath","selector":"//*[@id=\'nickname:nickname:\']/div/
input"}' ; Stacktrace: Method WebDriverError threw an error in
file:///c:/docume~1/ron/locals~1/temp/tmp2ifs6g/extensions/fxdr...@googlecode.com/resource/modules/utils.js

----------------------------------------------------------------------
Ran 1 test in 67.266s

FAILED (errors=1)

Traceback (most recent call last):
File "C:\Python27\PythonPrograms\SeleniumTestCases
\YahooCreateContact.py", line 67, in <module>
unittest.main()
File "C:\Python27\lib\unittest\main.py", line 95, in __init__
self.runTests()
File "C:\Python27\lib\unittest\main.py", line 231, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: True

Tony

unread,
Oct 18, 2011, 5:51:24 PM10/18/11
to Selenium Users
Webdriver probably dones't like your xpath, try something like :
-->driver.find_element_by_xpath("//input[@id='thatInputId']")

Don't use tools to get xpath because they may use different syntex and
webdriver fails to read it.
I personally only use tools to verify.
> input").send_keys("jwil...@hotmail.com")
> file:///c:/docume~1/ron/locals~1/temp/tmp2ifs6g/extensions/fxdri...@googlecode.com/resource/modules/utils.js

ron4563

unread,
Oct 18, 2011, 6:28:54 PM10/18/11
to Selenium Users
Tony,

Thanks for your reply. I tried what you suggested but it does not work
for
Here is a snippet of the XHTML. As you can see from the snippet below,
the input tag section has a dynamic id for the nickname field which I
cannot use. Any other suggestions.

<fieldset id="yui_3_2_0_1_13189760107191301" class="fs-lg">
<legend>Basics</legend>
<div id="nickname:nickname:" class="field label-left start-field-
here">
<label for="nickname">Nickname:</label>
<div id="yui_3_2_0_1_13189760107191298">
<input id="yui_3_2_0_1_13189760107191295" class="field-lg" type="text"
value="" name="nickname"/>
</div>
</div>

I was surprised you were against using the XPather tool to build the
xpath. I thought the tool was developed for finding the xpaths and to
make it easier to construct them.

As I indicated earlier I am new to this so I have a lot to learn.

Thanks again,

Ron


On Oct 18, 5:51 pm, Tony <weiweizhang...@gmail.com> wrote:
> Webdriver probably  dones't like your xpath, try something like :
> -->driver.find_element_by_xpath("//input[@id='thatInputId']")
>
> Don't use tools to get xpath because they may use different syntex and
> webdriver fails to read it.
> I personally only use tools to verify.
>

Tony

unread,
Oct 18, 2011, 7:52:03 PM10/18/11
to Selenium Users
Hi, Ron

I prefer clean xpath. it's easy to read and more efficient. Can you
try "//input[@name='nickname']"?

ron4563

unread,
Oct 19, 2011, 11:51:06 AM10/19/11
to Selenium Users
Tony,

Unfortunately, that does not work either. I am at a loss as to why
these xpaths don't work. They seem very unpredictable and unreliable
to me. Any other suggestions or can you point me somewhere to
investigate further. Thanks very much for your help.

Ron

On Oct 18, 7:52 pm, Tony <weiweizhang...@gmail.com> wrote:
> Hi, Ron
>
>    I prefer clean xpath. it's easy to read and more efficient. Can you
> try "//input[@name='nickname']"?
>

ron4563

unread,
Oct 19, 2011, 5:44:05 PM10/19/11
to Selenium Users
Tony,

I did some more investigation and it seems that your suggested xpath=//
input[@name='nickname'] does work using Firepath. However, it still
does not work in my webdriver test case. What I discovered is what I
suspected from the beginning and mentioned in my first post, that the
entry fields are in a different iframe which are not accessible from
the top window. In Firepath it is easy to switch to the iframe and it
shows me the name of the iframe (iframe#abIframeTab). I think I need
to switch to the iframe in the test case code before attempting to
enter data in the fields, but I am not sure how to do it. I believe
you have to use a selectFrame command but am not sure how to specify
an xpath locator for the iframe since it does not seem to accept the
iframe name. Can you recommend how to do this or is there some other
alternative that you or anyone else can suggest?

Thanks in advance for any help.

Ron

Josh

unread,
Oct 19, 2011, 5:54:48 PM10/19/11
to seleniu...@googlegroups.com
I realize that you are looking for a solution in java, however I created a solution for this in C# a few weeks ago...

I wrote a recursive function in C# that navigates all iframes in a page searching for the By argument passed in originally:

        public static IWebElement FindElementRecursive(this IWebDriver driver, By by)
        {
            string windowName = driver.CurrentWindowHandle;
            IWebElement found = null;
            try
            {
                found = driver.FindElement(by);
            }
            catch
            {
                List<IWebElement> frames = driver.FindElements(By.TagName("iframe")).ToList();
                foreach (var frame in frames)
                {
                    found = driver.SwitchTo().Frame(frame.GetAttribute("id")).FindElementRecursive(by);
                    if (found != null) return found;
                }
            }
            driver.SwitchTo().Window(windowName);
            return found;
        }

I don't know the equivalent code in Java, but this might help you!

ron4563

unread,
Oct 27, 2011, 2:45:48 PM10/27/11
to Selenium Users
Josh,

Sorry late getting back to you. I was successfully able to access the
fields when I used the SwitchToFrame command
(driver.switch_to_frame("abIframeTab"), Thanks for the suggestion.

Ron

Phil Albu

unread,
Aug 13, 2018, 1:27:16 AM8/13/18
to Selenium Users
Wow, I had been trying to figure out why I wasn't able to find an element by xpath when my query was clearly correct... didn't think about iframes though! Great tip!! Once I switched to the iframe, everything worked perfectly. :)
Reply all
Reply to author
Forward
0 new messages