Selenium c#无法使用FindElements获取列表
本文关键字:FindElements 获取 列表 Selenium | 更新日期: 2023-09-27 18:10:06
0、c#和NUnit.
我面临的问题是,我需要从span中取出值并存储到数组列表中。
我正在使用下面的代码获取报价单
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的手表。
- [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
我需要获取'Text'与上述手表给出的价格。
对不起,如果我不清楚,我对这个有点陌生
终于修复了
问题是用于存储元素的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;