0

我已经在这个很长一段时间超过2个月。它是从Selenium的下拉框中选择的。每次我尝试运行脚本时,都会报告它找不到该元素,可能是什么问题。 这是我的代码:硒webdriver从下拉列表中选择

driver.FindElement(By.CssSelector(UserGroupsConstants.UserGroupType)).Click(); 
driver.FindElement(By.CssSelector(UserGroupsConstants.UserGroupTypeSearch)).SendKeys("User Role"); 

这是我试过,但没有工作,以及

SelectElement se = new SelectElement(driver.FindElement(By.CssSelector(UserGroupsConstants.UserGroupType))); 
se.SelectByText("User Role"); 

这是下拉 enter image description here enter image description here

+0

也许它被视为一种价值。尝试安装萤火虫,看看“用户角色”是否被视为一个值或文本。或者你可以尝试:select se = new Select(driver.findElement(By.id(“yourIdHere”))); se.selectByVisibleText(“用户角色”);并确保您导入了“选择”库。还有一件事。如果它是一个值,你只需改变selectByValue(“用户角色”) –

回答

1

的图像在第二码查看页面源代码检查在选择标签中定义的下拉字段或按钮标签。如果在一个<select>标签定义下拉下面的脚本应该工作:

final Select droplist = new Select(driver.findElement(By.xpath("xpath-expression of dropdown selection"))); 
      droplist.selectByValue("User Role");    

如果下拉被定义<button>标签作为一个先进的选择,下面的代码可以帮助你。

driver.findElement(By.xpath("dropdown button xpath-expression")).click(); 
    WebElement ele = driver.findElement(By.xpath("xpath-expression of userrole")); 
    Actions action = new Actions(driver); 
    action.moveToElement(ele).click().build().perform(); 
+0

这显然不是一个标准的选择标签。您只能使用'Select'类作为'