Selenium does not find the item

I am trying to select a text field and enter text into it through the 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 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 anything special about the $ and _ characters, are these fields? Why can't selenium find these elements?

+4
source share
3 answers

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

To find out which windows are available, run:

 driver.window_handles 

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

 driver.switch_to_window(driver.window_handles[i]) 
+4
source

You have additional double inverted commas in the 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 you are not sure about $ and _ , use single inverted commas, something like this

 driver.find_element_by_name('mLayout$ctl00$ctl00$6$16$ctl00$Database') 
0
source

The idea is as follows. If you cannot find an element by the whole name, I would try to find it by part of the name. So I would try this approach:

Attribute A of an element, where A contains 't'

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

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

So it will be something

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

Thus, I usually check in cases where I am not sure about my locator: enter image description here

0
source

Source: https://habr.com/ru/post/1441019/


All Articles