1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java + Selenium Keyword Bot (Need Help)

Discussion in 'Programming' started by kittykut, Dec 31, 2016.

  1. kittykut

    kittykut Jr. VIP Jr. VIP

    Joined:
    Feb 9, 2016
    Messages:
    505
    Likes Received:
    215
    Gender:
    Male
    Location:
    localhost
    Hello Everyone :)
    I am looking for some help with my very simple keyword bot.

    It seems to work perfectly except for one issue:
    When It closes the driver and tries to restart using the proxy information I give it it starts without the proxy.

    Any help would be wonderful, I think it should be pretty simple

    Code:
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.FileSystems;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Proxy;
    import org.openqa.selenium.Proxy.ProxyType;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
        public class idek {
           
            // Keyword and proxy variables
            static String keyword;
            static String proxy;
           
        public static void main(String[] args) throws InterruptedException, IOException {
           
        // Creates an array of keywords called arr
        Path path = FileSystems.getDefault().getPath("src/info", "keywords.txt");
        List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
        String[] arr = lines.toArray(new String[lines.size()]);
       
        // Creates an array of proxies called proxies
        Path path1 = FileSystems.getDefault().getPath("src/info", "proxies.txt");
        List<String> linez = Files.readAllLines(path1, StandardCharsets.UTF_8);
        String[] proxies = linez.toArray(new String[linez.size()]);
       
        // Sets web driver
        System.setProperty("webdriver.gecko.driver","C:\\Users\\skr1ptk1tty\\Desktop\\geckodriver.exe");
    
        // loop through all keywords changing proxy each time
        for(int i = 0; i<arr.length; i++){
            proxy = proxies[i].substring(1);
            keyword = arr[i].substring(1);
            passTraffixThroughProxyTest(keyword, proxy);
        }
        }
    
        public static void passTraffixThroughProxyTest(String keyword, String proxy) throws InterruptedException {
       
            DesiredCapabilities capability = new DesiredCapabilities();
            addProxyCapabilities(capability, proxy, proxy, proxy);
            FirefoxDriver driver = new FirefoxDriver(new DesiredCapabilities(capability));
           
            // Go to MOZ.com and print keyword difficulty
            driver.get("https://moz.com/explorer/overview?q="+keyword);
            Thread.sleep(5000);
            String getDifficulty = driver.findElement(By.xpath("//*[@id=\"main\"]/div[2]/div[1]/section/div/section/section/section[1]/div[2]/span")).getText();
            System.out.println("Keyword Difficulty for "+keyword+" is: "+getDifficulty);
           
            //Close Driver
            driver.close();
            driver.quit();
            }
         
        public static DesiredCapabilities addProxyCapabilities(DesiredCapabilities capability, String httpProxy, String sslProxy,
        String ftpProxy) {
            Proxy proxy = new Proxy();
            proxy.setProxyType(ProxyType.MANUAL);
            proxy.setHttpProxy(httpProxy);
            proxy.setSslProxy(sslProxy);
            proxy.setFtpProxy(ftpProxy);
           
            capability.setCapability(CapabilityType.PROXY, proxy);
            capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            return capability;
            }
    
    }
    
    thank you for reading
     
  2. MrRandQm

    MrRandQm Registered Member

    Joined:
    Oct 4, 2016
    Messages:
    53
    Likes Received:
    15
    Code:
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", 1); //1 stands for http proxy
            profile.setPreference("network.proxy.http", proxyhost);
            profile.setPreference("network.proxy.http_port", proxyport);
    Driver d = new Driver(profile);
    I use this approach and it works for me
     
  3. MrRobot15

    MrRobot15 Newbie

    Joined:
    Dec 21, 2016
    Messages:
    28
    Likes Received:
    8
    Gender:
    Male
    Occupation:
    Freelance- Software enthusiast
    Location:
    Everywhere
    What exactly is your problem? Exception is thrown ? You need to tell us the problem so we can help you solve it
     
  4. Crawlie

    Crawlie Registered Member

    Joined:
    Jan 2, 2017
    Messages:
    50
    Likes Received:
    8
    Gender:
    Male
    Also, you might wanna look into HtmlUnit, it's more lightweight and faster than Selenium.