user1253952
user1253952

Reputation: 1597

selenium not finding the element

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?

Upvotes: 4

Views: 12667

Answers (3)

user1253952
user1253952

Reputation: 1597

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

Upvotes: 7

eugene.polschikov
eugene.polschikov

Reputation: 7339

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

Upvotes: 0

Amey
Amey

Reputation: 8548

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

Upvotes: 0

Related Questions