Created
November 22, 2013 15:44
-
-
Save mikeblakeuk/7601961 to your computer and use it in GitHub Desktop.
Sample Selenium SEO searcher.
Given target urls and a list of search, look for the page/element ranking.
It uses Selenium so hopefully google won't think it's automated.
Need to save the results and stuff...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
looking for 'mikeblakeuk' at 'http://www.google.co.uk' | |
About 22,600 results (0.16 seconds) | |
10 cite elements on page 1 | |
Page 2 of about 22,600 results (0.10 seconds) | |
10 cite elements on page 2 | |
Page 3 of about 22,600 results (0.11 seconds) | |
10 cite elements on page 3 | |
Page 4 of about 22,600 results (0.12 seconds) | |
10 cite elements on page 4 | |
Found 'www.zoominfo.com/p/Mike-Blake/2461152' element 3, Page 4 | |
Page 5 of 47 results (0.11 seconds) | |
7 cite elements on page 5 | |
Last Page | |
looking for 'blakeitsolutions blake' at 'http://www.google.co.uk' | |
About 197 results (0.21 seconds) | |
10 cite elements on page 1 | |
Found 'mikeblakeuk.wordpress.com/blakeitsolutions/' element 3, Page 1 | |
Page 2 of about 193 results (0.17 seconds) | |
10 cite elements on page 2 | |
Page 3 of about 193 results (0.18 seconds) | |
10 cite elements on page 3 | |
Page 4 of about 193 results (0.26 seconds) | |
10 cite elements on page 4 | |
Page 5 of 42 results (0.18 seconds) | |
2 cite elements on page 5 | |
Last Page | |
looking for 'mikeblake uk blake it solutions' at 'http://www.google.co.uk' | |
About 1,040,000 results (0.20 seconds) | |
10 cite elements on page 1 | |
Found 'mikeblakeuk.wordpress.com/blakeitsolutions/' element 6, Page 1 | |
Page 2 of about 1,040,000 results (0.18 seconds) | |
10 cite elements on page 2 | |
Found 'www.zoominfo.com/p/Mike-Blake/2461152' element 6, Page 2 | |
All targets found. Stopping | |
(1min of processing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using NUnit.Framework; | |
using OpenQA.Selenium; | |
using OpenQA.Selenium.Firefox; | |
using OpenQA.Selenium.Remote; | |
namespace SeleniumTests | |
{ | |
[TestFixture] | |
public class Test | |
{ | |
private readonly RemoteWebDriver driver = new FirefoxDriver(); | |
[Test] | |
public void Tests() | |
{ | |
var targetUrls = new[] | |
{ | |
"mikeblakeuk.wordpress.com/blakeitsolutions", | |
"www.zoominfo.com" | |
}; | |
var keywords = new[] | |
{ | |
"mikeblakeuk", | |
"blakeitsolutions blake", | |
"mikeblake uk blake it solutions" | |
}; | |
foreach (var keyword in keywords) | |
{ | |
Test1(keyword, targetUrls); | |
Console.WriteLine(); | |
} | |
driver.Quit(); | |
} | |
public void Test1(string keywords, string[] targetUrls) | |
{ | |
var baseURL = "http://www.google.co.uk"; | |
driver.Navigate().GoToUrl(baseURL); | |
Console.WriteLine("looking for '{0}' at '{1}'", keywords, baseURL); | |
var foundCount = 0; | |
for (int page = 0; page < 5; page++) | |
{ | |
driver.Navigate().GoToUrl(baseURL + string.Format("/#q={0}&start={1}", keywords.Replace(' ', '+'), page * 10)); | |
WaitForElement(By.XPath("//div[@id='ires']")); | |
var searchResultsElements = driver.FindElements(By.XPath("//div[@id='ires']//cite")); | |
var stats = driver.FindElement(By.XPath("//div[@id='resultStats']")); | |
Console.WriteLine(stats.Text); | |
Console.WriteLine("{0} cite elements on page {1}", searchResultsElements.Count, page + 1); | |
var i = 0; | |
foreach (var webElement in searchResultsElements) | |
{ | |
foreach (var targetUrl in targetUrls) | |
{ | |
if (webElement.Text.Contains(targetUrl)) | |
{ | |
Console.WriteLine("Found '{2}' element {0}, Page {1}", (i + 1), page + 1, webElement.Text); | |
foundCount++; | |
} | |
} | |
if (foundCount == targetUrls.Length) | |
{ | |
Console.WriteLine("All targets found. Stopping"); | |
return; | |
} | |
i++; | |
} | |
var nextButton = driver.FindElements(By.XPath("//span[text()='Next']")); | |
if (nextButton.Count == 0) | |
{ | |
Console.WriteLine("Last Page"); | |
return; | |
} | |
} | |
} | |
private void WaitForElement(By xPath) | |
{ | |
for (int second = 0; ; second++) | |
{ | |
if (second >= 50) | |
Assert.Fail("timeout"); | |
try | |
{ | |
if (driver.FindElements(xPath).Count > 0) | |
return; | |
} | |
catch | |
{ | |
} | |
Thread.Sleep(100); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment