【问题标题】:selenium not finding the element硒找不到元素
【发布时间】:2025-04-14 16:10:02
【问题描述】:

我正在尝试选择一个文本框并通过 selenium Web 驱动程序在其中输入文本。 html如下:

</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>

我已经尝试了以下

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

$ 和 _ 字符是字段有什么特别之处吗?为什么 selenium 不能定位这些元素?

【问题讨论】:

标签: python selenium webdriver selenium-webdriver


【解决方案1】:

解决方案:确保您在正确的窗口中。在此步骤之前的步骤中,我单击了一个打开新窗口的链接,并且我认为该窗口将自动成为活动窗口。

要查看哪些窗口可用,请运行:

driver.window_handles

这会返回一个列表。记下要更改到的窗口,索引为 i。然后更改窗口,运行:

driver.switch_to_window(driver.window_handles[i])

【讨论】:

    【解决方案2】:

    find_element_by_name("" 之后的第二行中有一个额外的双引号

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

    改成

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

    如果不确定$_,则使用单引号,类似这样的

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

    【讨论】:

    • 这仍然会导致相同的错误:selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"mLayout$ctl00 $ctl00$6$16$ctl00$Database"}'
    • 你试过用单引号吗??
    • 该元素是否实际出现在页面上?元素的名称是动态创建的吗?我无法想象为什么有人会真正命名一个文本字段。
    【解决方案3】:

    想法如下。如果您无法通过全名定位元素,我会尝试通过名称的一部分来定位它。 所以我会尝试这种方法:

    元素的属性A,其中A包含't'

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

    css: 呐{Se:css=E[A*='t']@A} 拍摄here

    原来如此

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

    以这种方式,我通常会在我对定位器不自信的情况下进行验证:

    【讨论】:

    • 会不会是这样的:driver.find_element_by_xpath("//input[contains(@name,'ctl00$Database')]") 我仍然得到一个错误:selenium.common.exceptions。 NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"//input[contains(@name,\'ctl00$Database\')]"}'
    • 简单的 "driver.find_element_by_xpath("input[contains(@name,'Database')" 怎么样?不要忘记验证在 firepath 中找到元素的定位器,在 fox 中的 firebug 插件跨度>