【问题标题】:Selenium C# unable to get the list using FindElementsSelenium C# 无法使用 FindElements 获取列表
【发布时间】:2015-11-20 17:49:49
【问题描述】:

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 选择器获得的元素中获取文本。

下面是视觉工作室的手表。

    -       [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

我需要获取上面手表中给出的价格的“文本”。

如果不清楚,抱歉。我对此有点陌生。

【问题讨论】:

  • 一旦有了元素,productprice.Text 有什么问题?
  • 它没有提供该选项。它会引发无扩展错误。 string a = productprice.Text; 也尝试创建一个数组列表,但它仍然无法正常工作。 ` ArrayList pp = new ArrayList();变种网址 = ""; foreach (IWebElement link in productprice) { //string text = link.Text; url = link.GetAttribute("Text"); pp.Add(url); } `
  • 你试过用 xpath 找到吗? var productprice = driver.FindElements(By.XPath("//div[@class='price.mb-10.text-right']"));只保证错误不在选择器中
  • 是的,我试过了,还是不行。查看下面的代码——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.FindElements(By.XPath("//div[@class='price.mb-10.text-right']")); var a = productprice.Text;
  • 您的 sn-p 中的 html 看起来并不完全有效

标签: c# html css visual-studio selenium


【解决方案1】:

终于解决了。

问题在于,用于存储元素的 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2020-10-16
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多