0、C# 和 NUnit。
我面临的问题是我需要从跨度中取出值并存储到数组列表中。
我正在使用下面的代码来获取价目表
var productprice = driver.FindElements(By.CssSelector(".price.mb-10.text-right "));
来自下面给出的 HTML
<div class="pl-sec">
<ul>
<li class="row-fluid">
<li class="row-fluid">
<div class="span2">
<div class="span7 disc-content">
<div class="span3 price-content">
<div class="price mb-10 text-right"> £14.99</div>
<div class="full-width mb-20">
</div>
</li>
<li class="row-fluid">
<div class="span2">
<div class="span7 disc-content">
<div class="span3 price-content">
<div class="price mb-10 text-right"> £16.99</div>
<div class="full-width mb-20">
</div>
</li>
我确实获得了列表,但无法从使用 css 选择器获得的元素中获取文本。
下面是visual studio的 watch 。
- [0] {OpenQA.Selenium.Firefox.FirefoxWebElement} OpenQA.Selenium.IWebElement {OpenQA.Selenium.Firefox.FirefoxWebElement}
+ [OpenQA.Selenium.Firefox.FirefoxWebElement] {OpenQA.Selenium.Firefox.FirefoxWebElement} OpenQA.Selenium.Firefox.FirefoxWebElement
Displayed true bool
Enabled true bool
+ Location {X = 802 Y = 793} System.Drawing.Point
Selected false bool
+ Size {Width = 164 Height = 14} System.Drawing.Size
TagName "div" string
Text "£16.99" string
我需要获取上面 watch 中给定价格的“文本”。
抱歉,如果不清楚。我对此有点陌生。
最佳答案
终于修好了。
问题在于,用于存储元素的 var 将元素存储为数组列表,并且必须找到位置,然后挖掘 Text 属性。 以下是更正后的代码-
driver.FindElement(By.XPath("//div[4]/div[2]/ul/li[2]/label")).Click();
//This below codes gets the count of product displayed by checking the prince.
var productprice = driver.FindElements(By.CssSelector(".price.mb-10.text-right "));
//var productprice = driver.FindElement(By.XPath("//div[@class='price.mb-10.text-right']")).Text;
string strval = "";
strval = productprice[5].Text;
关于c# - Selenium C# 无法使用 FindElements 获取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32230859/