Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 计算单击次数并返回Selenium、Python中单击链接的浏览器url_Javascript_Python_Selenium - Fatal编程技术网

Javascript 计算单击次数并返回Selenium、Python中单击链接的浏览器url

Javascript 计算单击次数并返回Selenium、Python中单击链接的浏览器url,javascript,python,selenium,Javascript,Python,Selenium,我是Selenium和Python的初学者,尝试使用Python和Selenium Webdriver自动化横幅测试 我想记录点击次数,点击后需要检查URL是否登录到正确的页面,每次点击我必须保持5秒的延迟 import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from se

我是Selenium和Python的初学者,尝试使用Python和Selenium Webdriver自动化横幅测试

我想记录点击次数,点击后需要检查URL是否登录到正确的页面,每次点击我必须保持5秒的延迟

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class slTest(unittest.TestCase):

def test_santi(self):
    self.browser=webdriver.Ie("C:\Users\chethan\Documents\IEDriverServer.exe")
    self.browser.implicitly_wait(5)
    self.browser.get("http://mywebsite.com/banners/")
    vr=self.browser.execute_script("return initValues")
    print vr
    num=0
    res=self.browser.find_element_by_id("productcontainer")
    links=res.find_elements_by_tag_name("a")
    for link in links:
        wait = WebDriverWait(self.browser, 10)
        element = wait.until(EC.element_to_be_clickable((By.ID,link.get_attribute("id"))))
        element.click()

        print link.get_attribute("id")
        #print self.browser.current_url
        num=num+1

    print num
    self.browser.quit()


if __name__ == "__main__":
unittest.main()
num的值只能在单击时增加,并记录单击后加载的URL

(我假设您需要在更复杂的环境中执行此操作,而不是使用简单的计数和检查函数)

在这种情况下,您应该修改selenium的click方法:

clickMethod = getattr(selenium.webdriver.remote.webelement.WebElement, 'click')
if clickMethod.func_name != 'decorated':
    def decorated(*args, **kwargs):
        self.count += 1

        start_url = self.driver.current_url
        return_this = clickMethod(*args, **kwargs)

        ##
        # you should probably time.sleep() or use javascript to check that page is finished loading
        ##

        end_url = self.driver.current_url
        if end_url != start_url:
            self.clicked_url_list.append( end_url )

        return return_this

    setattr( selenium.webdriver.remote.webelement.WebElement, 'click', decorated )

我是一个初学者,我不知道你在说什么,请你详细说明一下如何使用上面提到的以及“setattr”来调用这个函数。setattr()用于修改类的属性,例如myClass.attribute。上面的代码使用修改/修饰的方法覆盖selenium的click方法,该方法将执行计数和url检查