4

I am trying to select a textbox and enter text in it through selenium web driver. The html is as follows:

</div><div>
    <input name="mLayout$ctl00$ctl00$6$16$ctl00$Database" type="text" value="Enter database name" maxlength="175" size="26" id="mLayout_ctl00_ctl00_6_16_ctl00_Database" accesskey="s" title="Go search this database" class="InputContent GhostText" onfocus="SearchBoxOnFocus(&#39;mLayout_ctl00_ctl00_6_16_ctl00_Database&#39;);" onkeypress="if(!__TextBoxOnKeyPress(&#39;mLayout$ctl00$ctl00$6$16$ctl00$GoButton&#39;,event.which)) { return false; }" />&nbsp;<input type="image" name="mLayout$ctl00$ctl00$6$16$ctl00$GoButton" id="mLayout_ctl00_ctl00_6_16_ctl00_GoButton" title="Go search database" src="http://images-statcont.westlaw.com/images/go_v602.gif" alt="Go search database" align="absmiddle" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;mLayout$ctl00$ctl00$6$16$ctl00$GoButton&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" style="height:18px;width:21px;border-width:0px;" />
</div><div>

I've tried the following

driver.find_element_by_id("mLayout_ctl00_ctl00_6_16_ctl00_Database")
driver.find_element_by_name("mLayout$ctl00$ctl00$6$16$ctl00$Database")
dbElement = WebDriverWait(driver, 20).until(lambda x : x.find_element_by_id("mLayout_ctl00_ctl00_6_16_ctl00_Database"))

Is there something special about the $ and _ characters is the fields? Why can't selenium locate these elements?

user1253952
  • 1,577
  • 8
  • 22
  • 34
  • Similar: [Selenium-Python find_element_by_link_text - unable to locate element](http://stackoverflow.com/q/18023678/55075) – kenorb May 18 '15 at 09:13

3 Answers3

7

Solution: make sure you are in the right window. In the step before this one, I had clicked on a link that opened a new window, and I had assumed that that window would automatically be the active one.

To see which windows are available, run:

driver.window_handles

This returns a list. Note the window you want to change to, with index i. To then change the window, run:

driver.switch_to_window(driver.window_handles[i])
user1253952
  • 1,577
  • 8
  • 22
  • 34
0

You have an additional double inverted commas in your second line, after find_element_by_name(""

driver.find_element_by_name(""mLayout$ctl00$ctl00$6$16$ctl00$Database")

Change it to

driver.find_element_by_name("mLayout$ctl00$ctl00$6$16$ctl00$Database")

and whenever not sure abt the $ and _ then use single inverted commas, something like this

driver.find_element_by_name('mLayout$ctl00$ctl00$6$16$ctl00$Database')
Amey
  • 8,470
  • 9
  • 44
  • 63
  • This still results in the same error: selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"mLayout$ctl00$ctl00$6$16$ctl00$Database"}' – user1253952 Oct 20 '12 at 21:42
  • did u try with the single inverted commas?? – Amey Oct 20 '12 at 21:44
  • is the element actually present on the page? is the name of the element dynamically created? I cant imagine any reason why someone would actually name a text field that. – Amey Oct 21 '12 at 13:49
0

the idea is in following. IF you are not able to located element by the whole name I would try to locate it by the part of the name. So i would try this approach:

Attribute A of element where A contains 't'

xpath: //E[contains(@A,'t')]/@A ⌦ {Se: //E[contains(@A,'t')]@A }

css: NA {Se: css=E[A*='t']@A } taken here

So it be something

driver.find_element_by_xpath("input[contains(@name,'ctl00$Database')]@name")

in that way i usually verify in cases I'm not confident about my locator: enter image description here

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
  • 1
    Would it be something like this: driver.find_element_by_xpath("//input[contains(@name,'ctl00$Database')]") I still get an error: selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"//input[contains(@name,\'ctl00$Database\')]"}' – user1253952 Oct 21 '12 at 11:05
  • how about simple "driver.find_element_by_xpath("input[contains(@name,'Database')" ? don't forget to verify found locator of the element in firepath, firebug addon in ffox – eugene.polschikov Oct 21 '12 at 15:56