我正在尝试迭代外部div中的3个div,然后从每个内部div中的h1元素中获取文本。
我的HTML看起来像这样:
<div view-role="outerList">
<div class="question">
<div class="question-header"></div>
<div class="question-container">
<div class="question-view">
<h1 class="question-title">
What is your name?
</h1>
</div>
</div>
</div>
<div class="question">
<div class="question-header"></div>
<div class="question-container">
<div class="question-view">
<h1 class="question-title">
What is your favorite color?
</h1>
</div>
</div>
</div>
<div class="question">
<div class="question-header"></div>
<div class="question-container">
<div class="question-view">
<h1 question-heading class="question-title">
How old are you?
</h1>
</div>
</div>
</div>
</div>
我的C#代码如下所示:
foreach (IWebElement question in all_questions)
{
string question_name = question.FindElement(By.CssSelector("h1.question-title")).Text;
Console.WriteLine(question_name);
}
就我而言,问题是每个有一类问题的div。所以总共有3个。
NUnit因NoSuchElementException
而失败OpenQA.Selenium.NoSuchElementException : no such element
(Session info: chrome=42.0.2311.152)
(Driver info: chromedriver=2.12.301325 (962dea43ddd90e7e4224a03fa3c36a421281abb7),platform=Windows NT 6.1 SP1 x86_64)
然而,当我调试我的C#测试并进入我的行,导致立即窗口中的错误时,我能够得到实际的元素。但是,在此之前,它确实也打印了套接字异常。
question.FindElement(By.CssSelector("h1.question-title"));
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
{OpenQA.Selenium.Remote.RemoteWebElement}
[OpenQA.Selenium.Remote.RemoteWebElement]: {OpenQA.Selenium.Remote.RemoteWebElement}
Displayed: true
Enabled: true
Location: {X = 902 Y = 390}
Selected: false
Size: {Width = 420 Height = 53}
TagName: "h1"
Text: "What is your name?"
我不明白为什么我得到套接字异常,即使我可以实际检索元素。
其他信息: 这是我的驱动程序实例化:
private IWebDriver driver;
driver = new ChromeDriver();