【问题标题】:Unable to get the text of webelement in selenium web driver for bootstrap-dropdown using xpath无法使用 xpath 获取用于引导下拉菜单的 selenium Web 驱动程序中的 webelement 文本
【发布时间】:2021-08-31 07:27:49
【问题描述】:

我想访问 SERVICES 中的每个引导下拉菜单,然后必须选择一项服务并单击该服务 参考这个网站,https://mybmtc.karnataka.gov.in/english

我使用这个 XPath

IList<IWebElement> menus = driver.FindElements(By.XPath(".//*[@id='navbar - default']/div/div[2]/ul/li[2]/a"));

这是检查元素代码: 服务

--> --> -->特许服务

【问题讨论】:

    标签: c# html selenium selenium-webdriver drop-down-menu


    【解决方案1】:

    您需要将鼠标悬停在服务上,使用以下 CSS:

    a[title='Services']
    

    然后使用link_text选择链接Chartered Services。

    悬停:

    Actions builder = new Actions(driver);
    Actions hover = builder.MoveToElement(driver.FindElement(By.CssSelector("a[title='Services']
    "))).Build().Perform();
    

    然后点击:

    driver.FindElement(By.LinkText("Chartered Services")).Click();  
    

    【讨论】:

    • 除此之外,您可以创建一个通用方法,传递两个参数,如要选择的菜单和菜单项,将这些变量用于动态定位器值。
    • @Akshay:有道理
    • 谢谢@cruisepandey,上面的代码正在运行
    猜你喜欢
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多