使用 Selenium 在元素中查找数据
Finding data in element using Selenium
我想在 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&ie=UTF8&qid=1427809716&sr=1-2&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 {
}
我还不能 post 图片。但它可以是 found here.
你的代码最后多了一个)
。在为你移除那个 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')]"));
text()不能如下使用
IList<IWebElement> AppName = select.FindElements(By.XPath("//ul[@id='s-results-list-atf']//h2[text()='Dropchord']"));
而是按如下方式使用它:
IList<IWebElement> AppName = select.FindElements(By.XPath("//ul[@id='s-results-list-atf']//h2[contains(text(),'Dropchord')]"));
你能试试像这样的 xpath:
//a[@title='Dropchord']/h2
在这里你可以看到包含 h2 元素的 a 标签有 title="Dropchord" 并且它的子标签是 h2.
试试这个:
select.FindElements(By.xpath("//*[@id='atfResults']//h2"));
我想在 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&ie=UTF8&qid=1427809716&sr=1-2&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 {
}
我还不能 post 图片。但它可以是 found here.
你的代码最后多了一个
)
。在为你移除那个 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')]"));
text()不能如下使用
IList<IWebElement> AppName = select.FindElements(By.XPath("//ul[@id='s-results-list-atf']//h2[text()='Dropchord']"));
而是按如下方式使用它:
IList<IWebElement> AppName = select.FindElements(By.XPath("//ul[@id='s-results-list-atf']//h2[contains(text(),'Dropchord')]"));
你能试试像这样的 xpath:
//a[@title='Dropchord']/h2
在这里你可以看到包含 h2 元素的 a 标签有 title="Dropchord" 并且它的子标签是 h2.
试试这个:
select.FindElements(By.xpath("//*[@id='atfResults']//h2"));