【问题标题】:Cannot locate option dropdown with value in selenium web driver无法在 selenium Web 驱动程序中找到具有值的选项下拉列表
【发布时间】:2021-10-17 03:58:04
【问题描述】:
   wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("LOGON_selectedUser")));
   
   WebElement mySelectElement = driver.findElement(By.id("LOGON_selectedUser"));
   
   mySelectElement.click();
   
   
     Select dropdown= new Select(driver.findElement(By.id("LOGON_selectedUser")));
  
     dropdown.selectByVisibleText("HOTLINE-FAMILY SAFETY - Acting Supervisor");

以下是我得到的错误:

无法找到带有文本的选项:热线 - 家庭安全 - 代理主管 我试过了。值位没有运气。 :(

但是,我可以使用语句 mySelectElement.click(); 来选择下拉菜单;但是当我尝试选择下拉菜单时,我一直无法找到该元素。请帮忙。

这是 DOM:

enter image description here

【问题讨论】:

    标签: selenium-webdriver


    【解决方案1】:

    该网页是使用 Angular 构建的。请尝试用Select class诱导Explicit waits

    Select dropdown = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@name='selectedUser' and @id='LOGON_selectedUser' and @onchange]"))));
    dropdown.selectByIndex(1);
    

    或者如果这不起作用,下面应该可以工作

    ((JavascriptExecutor) driver).executeScript("return document.getElementById('LOGON_selectedUser').selectedIndex = '" + index + "';)
    

    其中index 是一个变量,请尝试传递索引号,如 1、2、3 等...

    【讨论】:

    • 感谢下面的代码工作: ((JavascriptExecutor) driver).executeScript("return document.getElementById('LOGON_selectedUser').selectedIndex = '1' " );
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2018-01-18
    • 2016-12-25
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多