selenium not finding the element


Question

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
4
10/20/2012 9:40:31 PM

Accepted Answer

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])
4
11/5/2012 10:24:17 PM

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon