Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果ul中包含文本,请选中复选框_C#_Html_Checkbox_Selenium Webdriver_Webdriver - Fatal编程技术网

C# 如果ul中包含文本,请选中复选框

C# 如果ul中包含文本,请选中复选框,c#,html,checkbox,selenium-webdriver,webdriver,C#,Html,Checkbox,Selenium Webdriver,Webdriver,我需要选中/取消选中图像上的标记为蓝色复选框。问题是我必须检查复选框是否来自带有窗帘标签的列表。 我的脚本必须执行以下操作: 导航到设置。(沙丘) 获取所有菜单的名称。(沙丘) 取消选中第一个菜单中的选项,保存并检查此选项是否从菜单中隐藏 会计 物品费用 添加 使用制度; 使用System.Collections.Generic; 使用OpenQA.Selenium; 使用OpenQA.Selenium.Firefox; 使用NUnit.Framework;

我需要选中/取消选中图像上的标记为蓝色复选框。问题是我必须检查复选框是否来自带有窗帘标签的列表。 我的脚本必须执行以下操作:

  • 导航到设置。(沙丘)
  • 获取所有菜单的名称。(沙丘)
  • 取消选中第一个菜单中的选项,保存并检查此选项是否从菜单中隐藏

    
    会计
    
    • 物品费用
      • 添加
                      使用制度; 使用System.Collections.Generic; 使用OpenQA.Selenium; 使用OpenQA.Selenium.Firefox; 使用NUnit.Framework; 使用System.Linq; 命名空间控制台应用程序1 { 一级考试 { //静态变量 静态int行=1; 静态int列=1; 静态字符串OurXpath=“//form[@id='editUserForm']/div/div[2]/ng include/div[“+row+”]/div/ul[“+column+”]/li/div/span”; //检查元素是否存在 私人秘书长出席(由) { 尝试 { 驱动程序。最终删除(通过); 返回true; } 捕获(无接触元素异常) { 返回false; } } IWebDriver; [设置] 公共无效初始化() { 驱动程序=新的FirefoxDriver(); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); } [测试] public void OpenAppTest() { driver.Url=“*************”; //登录 driver.FindElement(By.Id(“用户名”)).SendKeys(*****); driver.FindElement(按名称(“密码”)).SendKeys(“****”); driver.FindElement(By.Id(“登录提交”))。单击(); //导航到用户菜单 driver.FindElement(By.LinkText(“用户”))。单击(); //导航到id为的用户 driver.FindElement(By.Id(“edit-btn-354”))。单击(); driver.FindElement(By.LinkText(“模块权限”))。单击(); List ModuleNames=新列表(); //将所有特权添加到列表中 List elementTexts=新列表(driver.FindElements(By.CssSelector(“[class*='text-modify-btn-fake-btn-btn-xs-menuParentBtn-documenter_-top']))‌​elect(iw=>iw.Text)); 字符串示例=元素文本[1]; 控制台写入线(示例); //这是我无法访问所需元素的点。我知道此部分正在/span中搜索/li。我不知道如何在该/ul的私密子级中搜索 FindElement(By.XPath(“//ul[contains(span,“+sample+”))]/li[1]”); //这是为了检查查找菜单是否正常工作。 elementtext.ForEach(i=>Console.WriteLine(“{0}\t”,i)); } [撕裂] 公共测试() { driver.Quit(); } } }
    请尝试以下代码:

    IWebElement element = driver.findElement(By.xpath("//span[contains(text(),'Articles Expenses')]/../../ul[1]/li/div/div/span/input")); /*select first element/checkbox from the first menu i.e., Articles Expenses */
    if (element.Selected) /* if checkbox is already selected */
    {
        element.Click(); /* uncheck the checkbox if already selected */
    }
    

    请提供您的代码(作为文本而不是图像)。请共享您尝试的selenium C#代码和您获得的异常跟踪。是否有其他与“包含”不同的方法可用于检查文本是否完全相同。因为如果我想计算ul元素,假设我的span文本是“文章费用”,它有5个ul元素。如果我有其他跨度,文本“Articles Expenses Types”有3个元素。count方法将给出包含文本“Articles Expenses”的这两个跨度的总和。int count=driver.findelelements(By.XPath(//span[contains(text(),“+SettingToCheck+”)]/../../ul”)).count;您可以按如下方式使用:
    //span[text()='texttocheck']/../../ul/
    。我第一次尝试时,它不起作用。一个小时后,我发现出于某种原因,文本所在的html文件接受“空格”文本前后的新行。再次感谢你的帮助。
    IWebElement element = driver.findElement(By.xpath("//span[contains(text(),'Articles Expenses')]/../../ul[1]/li/div/div/span/input")); /*select first element/checkbox from the first menu i.e., Articles Expenses */
    if (element.Selected) /* if checkbox is already selected */
    {
        element.Click(); /* uncheck the checkbox if already selected */
    }