我可以将属性传递给selenium webdriver

时间:2016-01-28 17:15:39

标签: python class selenium

我试图创建一个可以创建wedriver实例并为其添加代理的类,这只是我已经写过的一个愚蠢的版本,但我也试图创建一个等待元素在单击之前可见的方法,该属性不会传递给驱动程序对象。

编辑:代码实际上很难理解,所以我加入了全班,因为我正在研究它。我还将click_elem方法更改为静态方法,但现在即使元素可见,它也会超时。

class Driver:
    proxies = []

    @staticmethod
    def test_proxy(proxy):
        try:
            urllib.urlopen(
                "https://www.google.com",
                proxies={'http': proxy})
            return True
        except IOError:
            print "Connection error! (Check proxy)"
            return False
        else:
            return False

    def get_proxied_driver(self):
        for proxy in self.proxies:
            try:
                myproxy = self.proxies.pop()
                if self.test_proxy(myproxy):
                     proxy = Proxy({
                                'proxyType': ProxyType.MANUAL,
                                'httpProxy': myproxy,
                                'ftpProxy': myproxy,
                                'sslProxy': myproxy,
                                'noProxy': '' # set this value as desired
                                  })
                    driver     = webdriver.Firefox(proxy=proxy)###### chrome, phantom js & IE etc ##
                    is_working = raw_input('Is your proxy working? [y/n]: ') # use selenium assert
                    if is_working == 'y' or is_working == 'Y':
                        return driver
                    if is_working == 'n' or is_working == 'N':
                        driver.quit()
                        continue
                    if not is_working == 'y' or is_working == 'Y' or is_working == 'n' or is_working == 'N':
                        print 'Invallid'
            except:
                continue

    # needs working_proxies.txt in the same dir
    def get_proxies(self):
        with open('working_proxies.txt', 'rb') as working_proxies:
            for proxy in working_proxies:
                proxy.rstrip()
                self.proxies.append(proxy.decode('ascii'))

    def get_proxies_n_driver(self): # same as get_driver but it adds proxies from -
        self.get_proxies()          # working_proxies.txt to proxies list
        driver = self.get_proxied_driver()
        return driver

    @staticmethod
    def click_elem(driver, elem_type, elem):
        element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((elem_type, elem)))

        if elem_type == By.CLASS_NAME:
            find = find_element_by_class_name
        elem = driver.find(elem).click()

driver = Driver()
#driver.test_proxy('213.136.79.124:80')
new_driver = driver.get_proxies_n_driver()
new_driver.get("https://twitter.com/signup")
driver.click_elem(new_driver, By.CLASS_NAME, "submit_button")

我还有另一个脚本来测试代理,然后将它们添加到working_proxies.txt

0 个答案:

没有答案