现在,Yahoo Trending上的Selenium自动化块不会抛出此类元素异常

时间:2018-07-04 14:21:09

标签: selenium-webdriver

我正在尝试查找链接的数量,并从Yahoo网页上的“ Trending Now”块打印链接项。以下代码引发“找不到此类元素”异常。我在下面的代码中使用xpath。我也尝试了cssSelector。任何人都可以通过xpath或cssSelector帮助我。谢谢。

package com.practice.one;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LinksYahoo {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", 
"/Users/owner/desktop/chromedriver");

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.yahoo.com");
List<WebElement> yahooLinks= driver.findElements(By.tagName("a"));
System.out.println(yahooLinks.size());
   /*   
    for(int i=0;i<yahooLinks.size();i++) {
        yahooLinks.get(i).getText();
    }
    */


 WebElementrightsideLinks=driver.findElement(By.xpath("//ul[@class='M(0) 
 Mstart(-8px) ua-ie8_W(100%) ua-ie7_W(100%) blur_V(h) blur_Op(0) 
selected_Op(1) Trs($trendTrs)'/li]"));

List<WebElement>right_Links=rightsideLinks.findElements
(By.tagName("a"));
System.out.println(right_Links.size());
for (int i=0;i<right_Links.size();i++) {
right_Links.get(i).getText();

}
}

}

1 个答案:

答案 0 :(得分:0)

使用以下代码,您可以获取所有流行的新文本:

System.setProperty("webdriver.chrome.driver", /Users/owner/desktop/chromedriver");

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.yahoo.com");
List<WebElement> yahooLinks= driver.findElements(By.tagName("a"));
System.out.println(yahooLinks.size());
/*   
            for(int i=0;i<yahooLinks.size();i++) {
                yahooLinks.get(i).getText();
            }
 */


List<WebElement>right_Links = driver.findElements(By.xpath("//li[@class='trending-list selected']/ul/li/a/span/following-sibling::span"));
        System.out.println(right_Links.size());
        for (int i=0;i<right_Links.size();i++) {
            System.out.println(right_Links.get(i).getText());
        }