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?

1

3 Answers 3

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])
Sign up to request clarification or add additional context in comments.

Comments

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')

3 Comments

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"}'
did u try with the single inverted commas??
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.
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

2 Comments

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\')]"}'
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.