为Selenium创建Xpath以单击下拉菜单

时间:2018-04-21 06:52:11

标签: python css selenium

我试图在状态后选择一个下拉箭头菜单。

我正在尝试构建一个Xpath来点击它:

<a href="javascript:" class="btn btn3d selectionbtn" style="top:0px; left:136px; width:21px; height:21px;">

Xpath = driver.find_element_by_xpath('//*[id="WIN_3_7"]/div/a').click()

但它说

  

无法找到元素

enter image description here

以下是上图中的HTML代码段。

<div id="WIN_3_7" arid="7" artype="EnumSel" ardbn="Status" arlbox="0,4,104,17" class="df arfid7 ardbnStatus EnumSel" style="z-index:1928;top:173px; left:705px; width:262px; height:21px;" arwindowid="3">
<label id="label7" class="label f9" for="x-arid_WIN_3_7" style="top:4px; left:0px; width:104px; height:17px;">Status*</label><div class="selection" style="top:0px; left:105px; width:157px; height:21px;" arselmenu="[{v:&quot;Assigned&quot;},{v:&quot;Pending&quot;},{v:&quot;Waiting Approval&quot;},{v:&quot;Planning&quot;},{v:&quot;In Progress&quot;},{v:&quot;Completed&quot;},{v:&quot;Rejected&quot;},{v:&quot;Cancelled&quot;,l:&quot;Canceled&quot;},{v:&quot;Closed&quot;}]">
<input id="arid_WIN_3_7" type="text" class="text " readonly="" style="top:0px; left:0px; width:136px; height:21px;" title="Assigned">
<a href="javascript:" class="btn btn3d selectionbtn" style="top:0px; left:136px; width:21px; height:21px;">
<img class="btnimg" src="../../../../resources/images/mt_sprites.gif" alt="">
</a>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

要点击并展开下拉菜单,您可以使用以下代码行:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id="WIN_3_7"]/div[@class='selection']/a[@class='btn btn3d selectionbtn']/img[@class='btnimg']"))).click()

更新

作为替代方案,您还可以尝试click()标记<input>并展开下拉菜单,如下所示:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='text' and @id='arid_WIN_3_7']"))).click()