Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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/9/csharp-4.0/2.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在Selenium中定位具有相同类的元素#_Selenium_C# 4.0_Selenium Webdriver - Fatal编程技术网

使用c在Selenium中定位具有相同类的元素#

使用c在Selenium中定位具有相同类的元素#,selenium,c#-4.0,selenium-webdriver,Selenium,C# 4.0,Selenium Webdriver,我正在尝试访问ABC。我知道简单的By.ClassName(“bb”)在这里不起作用。我还可以如何访问此内容 <body> <div id="Frame"> <div class="bb"></div> <div class="bb">ABC</div> </div> </body> 基础知识 您可以使用下面的css选择器获取“ABC”的值 可以使用表达式查找或定位元素 示例:elemen

我正在尝试访问ABC。我知道简单的By.ClassName(“bb”)在这里不起作用。我还可以如何访问此内容

<body>
 <div id="Frame">
 <div class="bb"></div>
 <div class="bb">ABC</div>
 </div>
</body>

基础知识

您可以使用下面的css选择器获取“ABC”的值

可以使用表达式查找或定位元素

示例:
element=findElement(By.xpath(“您的xpath表达式”);

对于XML,请使用以下行。


element=findelelement(By.xpath(“/body/div/div[@class='bb']][node()]”;

在使用xpath的搜索中有一种方法可以做到这一点,但我不是xpath专家。我可以使用CSS选择器为您提供一个解决方案。基本上,您可以使用类bb获取所有div,然后搜索它们的文本以找到所需的文本

String searchText = "ABC";
IReadOnlyCollection<IWebElement> divs = driver.FindElements(By.CssSelector("div.bb"));
foreach (IWebElement div in divs)
{
    if (div.Text == searchText)
    {
        break; // exit the for and use the variable 'div' which contains the desired DIV
    }
}
String searchText=“ABC”;
IReadOnlyCollection divs=driver.FindElements(By.CssSelector(“div.bb”);
foreach(分区中的IWebElement分区)
{
if(div.Text==searchText)
{
break;//退出for并使用包含所需div的变量'div'
}
}

注意:只有当元素始终是第二个元素时,这才有效。为什么不添加“ABC”文本作为XPath中的搜索条件?这似乎是最佳选择。您当前的答案没有回答这个问题。感谢您的回答。这似乎不起作用。我的divs计数始终保持为0。上面的HTML是否正确?如果是,除非它位于IFRAME或隐藏或其他位置,否则应该可以。
String searchText = "ABC";
IReadOnlyCollection<IWebElement> divs = driver.FindElements(By.CssSelector("div.bb"));
foreach (IWebElement div in divs)
{
    if (div.Text == searchText)
    {
        break; // exit the for and use the variable 'div' which contains the desired DIV
    }
}