gpt4 book ai didi

c# - Selenium 单击下拉菜单中的标题

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:36 26 4
gpt4 key购买 nike

我正在使用 C# 开发 selenium 应用程序。我在网页上有一个下拉菜单。我希望 selenium 在单击下拉菜单后单击确切的名称。我现在可以成功单击下拉菜单,并且可以查看该菜单上的标题或选项。

我想要的是,我需要 selenium 来根据用户在首次打开应用程序时提供的内容来点击标题。实际上菜单按钮是不可见的,但是当我将鼠标悬停在名称上时,它是可见的。

Menu Button

我试过了

var menu = driver.FindElement(By.ClassName("ms-vb"));
SelectElement sel = new SelectElement(menu);
sel.SelectByText(text);

其中 text 是用户输入的文本。运行应用程序时会抛出类似

的错误

“元素应该被选中但是被img了”

我该如何解决这个问题?我的任务因此而暂停..

任何意见将不胜感激..

谢谢

HTML

我希望这是您需要的代码:

<table cellspacing="1" cellpadding="0" height="100%" onmouseover="OnMouseOverFilter(this)" class="ms-unselectedtitle"
sortfields="SortField=Unify%5fx0020%5fApp&amp;SortDir=Asc&amp;View=%7b696D6B29%2d5A5D%2d47F3%2d97CF%2dECBFDD1E2CE8%7d"
resulttype="" fieldtype="Lookup" displayname="Unify App" ctxnum="1" name="Unify_x0020_App" filterable="" filterdisable=""
sortdisable="" sortable="" style="width:100%;" id="msomenuid2">
<tbody>
<tr>
<td width="100%" nowrap="nowrap" class="ms-vb">
<a sortingfields="SortField=Unify%5fx0020%5fApp&amp;SortDir=Asc&amp;View=%7b696D6B29%2d5A5D%2d47F3%2d97CF%2dECBFDD1E2CE8%7d"
onclick="javascript:return OnClickFilter(this,event);" href="javascript:" title="Sort by Unify App"
onfocus="OnFocusFilter(this)" id="diidSortUnify_x0020_App">Unify App
<img width="1" border="0" height="1" alt="Use SHIFT+ENTER to open the menu (new window)." class="ms-hidden"
src="/_layouts/images/blank.gif" /></a>
<img border="0" alt="" src="/_layouts/images/blank.gif" />
<img border="0" alt="" src="/_layouts/images/blank.gif" />
</td>
<td style="position: absolute; right: 1px;" class="">
<img width="13px" alt="Open Menu" style="visibility: hidden;" src="/_layouts/images/menudark.gif" />
</td>
</tr>
</tbody>
</table>

最佳答案

如果它不是普通元素,则 SelectElement 类将不起作用。

这是我处理 javascript 下拉菜单的方式:

    //Click on the dropdown
driver.FindElement(By.ClassName("ms-vb")).Click();
//Wait for the result list to appear
wait.Until(x => x.FindElement(By.LinkText("Target link to click")));
//Click on the result you want
driver.FindElement(By.LinkText("Target link to click")).Click();

如果结果不是链接,您应该使用 xPath 通过其文本找到它。例如。

    driver.FindElement(By.XPath("//*[text()='Target text']")).Click();

希望对您有所帮助。

关于c# - Selenium 单击下拉菜单中的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17608457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com