在抓github时无法进入代码搜索

时间:2015-02-09 20:00:19

标签: java selenium github selenium-webdriver web-scraping

我正在尝试使用selenium web scraping模拟在线github搜索。我无法在代码部分中获得程序搜索。相反,它试图在存储库中搜索。

以下是代码。

FirefoxProfile p = new FirefoxProfile();
    p.setPreference("javascript.enabled", false);


    org.openqa.selenium.WebDriver driver = new FirefoxDriver();


    driver.get("https://github.com"); 

    WebElement element;

       element = driver.findElement(By.name("q"));



    element.sendKeys("hasRole()");

    element.submit();
    wait(driver);


   element =  driver.findElement(By.name("Code"));  //prev working line


    element.click();
    wait(driver);

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过部分链接文字找到链接:

element = driver.findElement(By.partialLinkText("Code"));

或者, by xpath ,检查网址中的type=Code部分:

element = driver.findElement(By.xpath("//a[contains(@href, 'type=Code')]"));

此外,您可能需要等待元素变为可见

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@href, 'type=Code')]")));