使用Selenium在元素中查找数据

时间:2015-04-02 07:37:36

标签: selenium selenium-webdriver selenium-ide

我想在Craigslist上的<h2>元素中找到元素“Dropchord”。

示例HTML:

<a class="a-link-normal s-access-detail-page a-text-normal" title="Dropchord" href="http://www.amazon.de/Double-Fine-Productions-Dropchord/dp/B00E0OK2X2/ref=sr_1_2?s=mobile-apps&amp;ie=UTF8&amp;qid=1427809716&amp;sr=1-2&amp;keywords=AS-Productions">
  <h2 class="a-size-base a-color-null s-inline s-access-title a-text-normal">Dropchord</h2>
</a>

我尝试了很多东西,例如:

try {
   //IList<IWebElement> AppName = select.FindElements(By.XPath("(//h2[contains(@class, 'a-size-base a-color-null s-inline s-access-title a-text-normal')])"));
   //IList<IWebElement> AppName = select.FindElements(By.XPath("//h2[@ class='a-size-base a-color-null s-inline s-access-title a-text-normal']"));
   //IList<IWebElement> AppName = select.FindElements(By.CssSelector("h2[class='a-size-base a-color-null s-inline s-access-title a-text-normal'])"));
   //IList<IWebElement> AppName = select.FindElements(By.CssSelector("h2.(a-size-base a-color-null s-inline s-access-title a-text-normal)"));
   //IList<IWebElement> AppName = select.FindElements(By.CssSelector("h2"));
   IList<IWebElement> AppName = select.FindElements(By.XPath(".//h2"));
   //IList<IWebElement> AppName = select.FindElements(By.XPath("//ul[@id='s-results-list-atf']//h2[text()='Dropchord']"));
   //IList<IWebElement> AppName = select.FindElements(By.CssSelector("//a[@ class='a-link-normal s-access-detail-page a-text-normal']//h2 [@ class='a-size-base a-color-null s-inline s-access-title a-text-normal']"));

    foreach (IWebElement Name in AppName){
        if (Name.Text == appname){
            // ...
        }
    }
} catch {

}

我还不能发布图片。但它可以是found here

3 个答案:

答案 0 :(得分:0)

  1. 您的代码最后还有一个)。在为您删除了那些shloud工作之后。

    IList<IWebElement> AppName = select.FindElements(By.XPath("(//h2[contains(@class, 'a-size-base a-color-null s-inline s-access-title a-text-normal')]"));
    
  2. text()不能如下使用

     IList<IWebElement> AppName = select.FindElements(By.XPath("//ul[@id='s-results-list-atf']//h2[text()='Dropchord']"));
    
  3. 改为使用如下:

    IList<IWebElement> AppName = select.FindElements(By.XPath("//ul[@id='s-results-list-atf']//h2[contains(text(),'Dropchord')]"));
    

答案 1 :(得分:0)

请你试试xpath,如:

  

//一个[@title =&#39; Dropchord&#39;] / H2

在这里你可以看到包含h2元素的标签有title =&#34; Dropchord&#34;它的孩子是h2。

答案 2 :(得分:0)

试试这个:

select.FindElements(By.xpath("//*[@id='atfResults']//h2"));
相关问题