使用firebug制作文本的问题制作了xpath或css选择器

时间:2012-04-30 15:49:07

标签: html xpath selenium css-selectors

我一直在尝试使用selenium在网页中解析这段文字,但无法弄清楚为什么我无法显示我想要的文字。页面上的代码片段就是这样

<div id="BVRRRatingOverall_Review_Display" class="BVRRRating BVRRRatingNormal BVRRRatingOverall">
<div class="BVRRLabel BVRRRatingNormalLabel"/>
<div class="BVRRRatingNormalOutOf">
<span class="BVRRNumber BVRRRatingNumber">4</span>
<span class="BVRRSeparatorText">out of</span>
<span class="BVRRNumber BVRRRatingRangeNumber">5</span>
</div>
</div>

我希望得到“4/5”的文字。我使用firebug来检查我的xpath / cssSelector是否有效但是selenium仍然找不到文本。我尝试过不同的waits和cssSelectors实现来尝试解决这个问题。似乎没什么用。我目前正在使用的代码是

String Url2 = "http://www.walmart.com/catalog/allReviews.do?product_id=16207347";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(Url2);
System.out.println("test" + driver.findElement(By.cssSelector(".BVRRSeparatorText")).getText());

我只是想在屏幕上打印“test out of”,但我只打印“test”。有谁知道我怎么能让这个工作?我环顾了互联网,但我找不到任何工作。

我也在测试这个案例的等效xpath是//*[@id='BVRRRatingOverall_Review_Display']/div[@class='BVRRRatingNormalOutOf']/span[@class='BVRRSeparatorText']

编辑*我认为问题是因为文本实际上从未在网页上显示。它的文本仅在悬停在页面上的元素上时才会出现。

1 个答案:

答案 0 :(得分:0)

您可以获取评级星形图像的替代文字。

Ruby语法是(不确定其他语言的确切语法,但应该存在相似的语法):

e = driver.find_element(:css, ".BVImgOrSprite")                                                    
puts e.attribute('alt')                                                                       
# => 4 out of 5