【问题标题】:Selenium cannot find ul Element that is visible on PageSelenium 找不到页面上可见的 ul 元素
【发布时间】:2021-02-26 18:16:57
【问题描述】:

我目前正在测试电子邮件服务,并且在打开过滤电子邮件的选项列表后,我希望能够自动单击列表中的选项。列表的代码是:

但是,selenium 找不到这个元素,即使我可以通过使用 CTRL+F 搜索 HTML 来找到它。我目前用来尝试查找并单击此列表元素的代码是:

      wait.until(ExpectedConditions.visibilityOfElementLocated(org.myorg.automation.Objects.ManageEmails.Locators.FilterList));
    Select dropdown = new Select(driver.findElement(org.myorg.automation.Objects.ManageEmails.Locators.FilterList));
    dropdown.selectByVisibleText("Unread");

列表的xpath是:

/html/body/div[7]/div/div/div/div/div/div/ul

任何帮助将不胜感激!

【问题讨论】:

    标签: java selenium


    【解决方案1】:

    问题是在这种情况下您没有选择,并且: Select dropdown = new Select();不会工作。您需要一个自定义方法来从该列表中选择一个值

     public class Testing {
    
        public WebDriver driver;
    
        @Test
        public void TestSomething() {
    
            driver = new ChromeDriver();
    
            driver.get("<the url where this list is present>");
    
            // assuming that the ul list is unique on the page if not find another way to get it       
            WebElement ulListParent = driver.findElement(By.xpath("//ul[contains(@class,'ms-ContextualMenu-list is-open')]"));
            
            SelectBasedOnValue(ulListParent, "Unread");
    
            driver.close();
    
        }
    
        public void SelectBasedOnValue(WebElement parentElement, String optionValue) {
    
            parentElement.findElement(By.xpath(String.format("//li[text()='%s']", optionValue))).click();
            
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-30
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2021-08-15
      • 2018-12-09
      • 1970-01-01
      相关资源
      最近更新 更多