我正在尝试自动化一个似乎使用框架的 Web UI。我无法在该 UI 中找到某些元素,这与其他没有框架概念开发的 UI 不同。 例如:我正在尝试单击下拉字段。当我使用“Firebug”检查该字段时,我看到了该下拉列表的 html 堆栈。
<frame border="0" marginheight="0" marginwidth="5" name="body" noresize=""
src="Pub_Docs/index.htm">
<body marginwidth="5" marginheight="0">
<form action="../dxnrequest.aspx?RequestName=TadiPost" method="POST">
<input type="hidden" name="TadiServer" value="TPASS"/>
<input type="hidden" name="TadiUrl" value="/testdx/curr_hols.call"/>
<div style="position: relative; left: 5px; top: 0px;">
<table width="100%" class="BKGND">
<tbody>
<tr>
<tr>
<td>
<div style="position: relative; left: 4px; top: 1px;">
<table width="100%" class="PARAMS">
<tbody>
<tr>
<td valign="TOP">
<table class="CLEAR">
<tbody>
<tr>
<td class="HEADER">Currency:</td>
<td>
<**select name="in_CUR_CODE" size="1">**
</td>
</tr>
<tr>
<tr>
<tr>
</tbody>
</table>
</td>
<td valign="TOP">
</tr>
<tr>
<tr>
</tbody>
Firebug 指向此下拉列表的 name="in_CUR_CODE。所以我认为这会起作用。
ccy_dd_locator = "//form[@method='POST']//select[@name='in_CUR_CODE']"
CCY_DD_LOCATOR_VALUE_XPATH = (By.XPATH, 'ccy_dd_locator_value')
frame = WebDriverWait(self.driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, 'body')))
WebDriverWait(self.driver, 15).until(EC.presence_of_element_located(CCY_DD_LOCATOR_VALUE_XPATH)).click()
但它没有找到下拉菜单,我收到“超时”异常。 selenium 中是否有不同的方法来定位使用框架的 UI 中的元素? 这似乎不适用于其他不使用框架的 UI。
非常感谢任何有助于理解这一点的帮助。
您应该将
ccy_dd_locator
作为 xpath 传递,而不是诸如 'ccy_dd_locator_value'
之类的字符串
driver.find_element(By.XPATH, ccy_dd_locator).click()
如果这不起作用,您应该先切换到
frame
,然后寻找元素
frame = driver.find_element(By.CSS_SELECTOR, 'frame[@src="Pub_Docs/index.htm"]')
driver.switch_to.frame(iframe)
ccy_dd_locator = "//form[@method='POST']//select[@name='in_CUR_CODE']"
driver.find_element(By.XPATH, ccy_dd_locator).click()