Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xpath 即使使用了正确的类名,我也无法捕获所有搜索结果链接和描述_Xpath_Selenium_Automation_Selector - Fatal编程技术网

Xpath 即使使用了正确的类名,我也无法捕获所有搜索结果链接和描述

Xpath 即使使用了正确的类名,我也无法捕获所有搜索结果链接和描述,xpath,selenium,automation,selector,Xpath,Selenium,Automation,Selector,我想从结果页面捕获所有搜索结果链接(搜索引擎:)和摘要。链接的类名是'yschttl spt',摘要的类名是'absr' 链接和摘要的源代码如下所示 final List<WebElement> links = driver.findElements(By.className("yschttl spt")); final List<WebElement> linksSummary = driver.findElements(By.className("abstr"));

我想从结果页面捕获所有搜索结果链接(搜索引擎:)和摘要。链接的类名是
'yschttl spt'
,摘要的类名是
'absr'

链接和摘要的源代码如下所示

final List<WebElement> links = driver.findElements(By.className("yschttl spt")); 
final List<WebElement> linksSummary = driver.findElements(By.className("abstr"));
链接:

`
摘要组:

<div id="yui_3_3_0_1_1301085039901338" class="abstr"><b id="yui_3_3_0_1_1301085039901337">Pune</b> is undoubtedly a great place to eat. Fergusson <b id="yui_3_3_0_1_1301085039901352">College</b> <b>Road</b> is full of budget eateries serving delicous hot food at nominal charges. For a range of multi-cuisine ...</div>
浦那无疑是一个美食的好地方。Fergusson College Road到处都是廉价餐厅,提供美味的热菜,收费不菲。对于一系列的多种美食。。。
我使用下面的代码行来捕获这两个内容(链接和摘要)

final List links=driver.findElements(By.className(“yschttl spt”);
最终列表链接汇总=driver.findElements(By.className(“absr”);
但现在一切都在运转

我还尝试使用以下XPath作为链接:

//a[以(@id,'yui')开头]

//a[@data bns='API']

我不能将ID作为一个整体使用,因为所有搜索结果链接的ID号都不相同

什么都没用。请帮忙


提前感谢。

我不使用selenium 2.0,但当我在Firefox XPath Checker中尝试
//a[@class='yschttl spt']
时,我在页面中看到了所有10个结果。我很快就要开始学习Selenium 2.0了,也许我可以试试…

主要的问题是您没有正确地询问类名。
class=“yschttl spt”
的实例表明,元素可以由两个类名来标识,
yschttl
spt
。它不会说类名是
yschttl spt
,因此请求.classname的
(“yschttl spt”)
总是失败的

请注意,@Tarun建议使用XPath的原因是XPath对HTML类名是什么或应该是什么没有概念。在XPath中,
@class
只指定属性的名称——基本语义为no


此外,请注意,类名可能不包含空格。有关指定类名的更多详细信息,请参阅HTML属性规范。

您确定没有任何默认名称空间声明吗?@Alejandro:我不明白。我刚开始学习,你能详细告诉我吗。谢谢很奇怪!!当我使用.className(“yschttl spt”)时,它不起作用,但当我使用XPath时,它返回正确的结果。非常感谢。但是现在我的问题是-为什么if不使用.className()的
By.className()
(好的。但是如果不想使用XPath,那么如果类名有空格,我们如何指定类名呢?再次,让我强调类名没有空格;您有两个不同的类名,因此使用
By.className
语法,您可以指定
By.className(“yschttl”)
By.className(“spt”)
。但是类名是'yschttl spt',所以我的问题是..当类名中有空格时,
By.className()
方法中我们必须如何使用?很抱歉,我还是没有理解。因为class属性指定了两个类名(yschttl和spt)您只能使用.className选择其中一个。如果确实需要查找具有两个类名的元素,则需要首先查找具有其中一个名称的元素,然后测试它的class属性是否也包含另一个。因此,它不是非常优雅。使用XPath匹配属性可能更简单值,但这种方法也存在缺陷:例如class=“yschttl spt”和class=“spt yschttl”显然是不同的属性,但它们指定了相同的类名。我明白你的意思,但我仍然感到困惑。
“yschttl spt”
是一个单独的类名。因此,你的意思是有空格的类名不能在
中由.className()使用
我必须始终使用
By.xpath()
final List<WebElement> links = driver.findElements(By.className("yschttl spt")); 
final List<WebElement> linksSummary = driver.findElements(By.className("abstr"));