c# - 下拉没有 id 也没有名字

标签 c# html selenium

我写下拉代码。

if (elementtype == "Id")
    new SelectElement(driver.FindElement(By.Id(element))).SelectByText(value);
if (elementtype == "Name")
    new SelectElement(driver.FindElement(By.Name(element))).SelectByText(value);

但我想选择一个没有 id 和 name 的下拉列表

<div aria-posinset="0" aria-setsize="3" role="option" id=":d" 
    class="goog-inline-block goog-flat-menu-button-caption">
    I am...
</div>
<div aria-hidden="true" class="goog-inline-block goog-flat-menu-button-dropdown">&nbsp;</div>

这是我写的类

public static void SelectDropDown(IWebDriver driver,string element,string value,string elementtype) { 
    // SelectElement selectElement = new SelectElement(); 
    if (elementtype == "Id") new     SelectElement(driver.FindElement(By.Id(element))).SelectByText(value); 
    if (elementtype == "Name") new     SelectElement(driver.FindElement(By.Name(element))).SelectByText(value); 
} 

我是这样使用它的:

seleniumsetmethods.SelectDropDown(driver, "Gender", "Man", "Id")

我想选择数量和性别。 https://accounts.google.com/signup?lp=1&hl=en

最佳答案

只需将您自己的类添加到 HTML 代码并通过 Java/JavaScript 引用它。删除 C# 标记,因为它不是 C#。

<div aria-posinset="0" aria-setsize="3" role="option" id=":d" 
class="goog-inline-block goog-flat-menu-button-caption gender-dropdown"> <-- custom class
I am...
</div>

另外,你有错误的月份下拉代码,是这样的:

<div class="goog-inline-block goog-flat-menu-button-caption month-dropdown"<- custom class id=":0" role="option" aria-setsize="12" aria-posinset="2">Month</div>

并且,假设您的 Java 代码可以正常工作,那么只需执行以下操作:

if (elementtype == "Class")
new SelectElement(driver.FindElement(By.ClassName(element))).SelectByText(value);

老实说,JavaScript 更容易......

<script>
    var gender = Document.getElementsByClassName('gender-dropdown')[0].innerHTML;

    var month = Document.getElementsByClassName('month-dropdown;)[0].innerHTML;
</script>

虽然你的问题令人困惑......

关于c# - 下拉没有 id 也没有名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025619/

相关文章:

c# - 空条件运算符对委托(delegate)和常规对象的功能是否相同?

javascript - Android video.duration 直到 video.play() 才会加载

javascript - 使用具有多个下拉列表的 jQuery "find"和 "filter"进行过滤

python - Selenium/Python 做 n 次

python - 如何在 Python Selenium 中实现类似 TestNG 的功能或在一个测试套件中添加多个单元测试?

c# - 如何让事件向事件处理程序发送参数?

c# - ReSharper 无法解析评论中的集合类型

c# - 将商店中的证书用于 RSACryptoServiceProvider

javascript - jQuery - 隐藏/显示不显示 div 并显示错误

java - 虽然当我执行 pageSource().contains.(text) 时,文本出现在 pagesource() 中,但它返回 false